1717use Magento \Quote \Api \GuestCartItemRepositoryInterface ;
1818use Magento \Quote \Model \QuoteIdMaskFactory ;
1919use Magento \Store \Model \StoreManagerInterface ;
20+ use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
21+ use Magento \Catalog \Model \ResourceModel \Product \Website \Link as ProductWebsiteLink ;
2022
2123/**
2224 * Plugin to update cart ID from request and validate product website assignment
@@ -35,7 +37,9 @@ public function __construct(
3537 private readonly ProductRepositoryInterface $ productRepository ,
3638 private readonly StoreManagerInterface $ storeManager ,
3739 private readonly QuoteIdMaskFactory $ quoteIdMaskFactory ,
38- private readonly CartRepositoryInterface $ cartRepository
40+ private readonly CartRepositoryInterface $ cartRepository ,
41+ private readonly ProductResource $ productResource ,
42+ private readonly ProductWebsiteLink $ productWebsiteLink
3943 ) {
4044 }
4145
@@ -74,23 +78,23 @@ private function validateProductWebsiteAssignment(CartItemInterface $cartItem):
7478 return ;
7579 }
7680
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 ;
9187 }
9288
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+ }
9498 }
9599
96100 /**
@@ -103,12 +107,12 @@ private function validateProductWebsiteAssignment(CartItemInterface $cartItem):
103107 */
104108 private function validateWebsiteAssignmentBySku (string $ sku , int $ storeId ): void
105109 {
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 ) {
110112 throw new LocalizedException (__ ('Product that you are trying to add is not available. ' ));
111113 }
114+ $ websiteIds = $ this ->productWebsiteLink ->getWebsiteIdsByProductId ($ productId );
115+ $ this ->checkProductInWebsite ($ websiteIds , $ storeId );
112116 }
113117
114118 /**
0 commit comments