17
17
use Magento \Quote \Api \GuestCartItemRepositoryInterface ;
18
18
use Magento \Quote \Model \QuoteIdMaskFactory ;
19
19
use Magento \Store \Model \StoreManagerInterface ;
20
+ use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
21
+ use Magento \Catalog \Model \ResourceModel \Product \Website \Link as ProductWebsiteLink ;
20
22
21
23
/**
22
24
* Plugin to update cart ID from request and validate product website assignment
@@ -35,7 +37,9 @@ public function __construct(
35
37
private readonly ProductRepositoryInterface $ productRepository ,
36
38
private readonly StoreManagerInterface $ storeManager ,
37
39
private readonly QuoteIdMaskFactory $ quoteIdMaskFactory ,
38
- private readonly CartRepositoryInterface $ cartRepository
40
+ private readonly CartRepositoryInterface $ cartRepository ,
41
+ private readonly ProductResource $ productResource ,
42
+ private readonly ProductWebsiteLink $ productWebsiteLink
39
43
) {
40
44
}
41
45
@@ -74,23 +78,23 @@ private function validateProductWebsiteAssignment(CartItemInterface $cartItem):
74
78
return ;
75
79
}
76
80
77
- $ storeId = (int )($ cartItem ->getStoreId () ?? 0 );
78
- if (!$ storeId ) {
79
- $ maskedQuoteId = $ cartItem ->getQuoteId ();
80
- $ quoteIdMask = $ this ->quoteIdMaskFactory ->create ()->load ($ maskedQuoteId , 'masked_id ' );
81
- $ quoteId = (int )$ quoteIdMask ->getQuoteId ();
82
- if (!$ quoteId ) {
83
- return ;
84
- }
85
- try {
86
- $ quote = $ this ->cartRepository ->get ($ quoteId );
87
- $ storeId = (int )$ quote ->getStoreId ();
88
- } catch (NoSuchEntityException ) {
89
- throw new LocalizedException (__ ('Product that you are trying to add is not available. ' ));
90
- }
81
+ $ maskedQuoteId = $ cartItem ->getQuoteId ();
82
+ $ quoteIdMask = $ this ->quoteIdMaskFactory ->create ()->load ($ maskedQuoteId , 'masked_id ' );
83
+ $ quoteId = $ quoteIdMask ->getQuoteId ();
84
+
85
+ if (!$ quoteId ) {
86
+ return ;
91
87
}
92
88
93
- $ this ->validateWebsiteAssignmentBySku ($ sku , $ storeId );
89
+ try {
90
+ $ quote = $ this ->cartRepository ->get ($ quoteId );
91
+ $ storeId = $ quote ->getStoreId ();
92
+ // Product not in quote yet
93
+ $ this ->validateWebsiteAssignmentBySku ($ sku , $ storeId );
94
+
95
+ } catch (NoSuchEntityException ) {
96
+ throw new LocalizedException (__ ('Product that you are trying to add is not available. ' ));
97
+ }
94
98
}
95
99
96
100
/**
@@ -103,12 +107,12 @@ private function validateProductWebsiteAssignment(CartItemInterface $cartItem):
103
107
*/
104
108
private function validateWebsiteAssignmentBySku (string $ sku , int $ storeId ): void
105
109
{
106
- try {
107
- $ product = $ this ->productRepository ->get ($ sku , false , $ storeId );
108
- $ this ->checkProductInWebsite ($ product ->getWebsiteIds (), $ storeId );
109
- } catch (NoSuchEntityException ) {
110
+ $ productId = (int )$ this ->productResource ->getIdBySku ($ sku );
111
+ if (!$ productId ) {
110
112
throw new LocalizedException (__ ('Product that you are trying to add is not available. ' ));
111
113
}
114
+ $ websiteIds = $ this ->productWebsiteLink ->getWebsiteIdsByProductId ($ productId );
115
+ $ this ->checkProductInWebsite ($ websiteIds , $ storeId );
112
116
}
113
117
114
118
/**
0 commit comments