1212use Magento \Framework \Exception \LocalizedException ;
1313use Magento \Framework \Exception \NoSuchEntityException ;
1414use Magento \Framework \Webapi \Rest \Request as RestRequest ;
15- use Magento \Quote \Api \CartRepositoryInterface ;
1615use Magento \Quote \Api \Data \CartItemInterface ;
1716use Magento \Quote \Api \GuestCartItemRepositoryInterface ;
1817use Magento \Quote \Model \QuoteIdMaskFactory ;
1918use Magento \Store \Model \StoreManagerInterface ;
2019
2120/**
22- * Plugin to update cart ID from request and validate product website assignment
21+ * Update cart id from request param
2322 */
2423class UpdateCartId
2524{
@@ -28,42 +27,42 @@ class UpdateCartId
2827 * @param ProductRepositoryInterface $productRepository
2928 * @param StoreManagerInterface $storeManager
3029 * @param QuoteIdMaskFactory $quoteIdMaskFactory
31- * @param CartRepositoryInterface $cartRepository
3230 */
3331 public function __construct (
3432 private readonly RestRequest $ request ,
3533 private readonly ProductRepositoryInterface $ productRepository ,
36- private readonly StoreManagerInterface $ storeManager ,
37- private readonly QuoteIdMaskFactory $ quoteIdMaskFactory ,
38- private readonly CartRepositoryInterface $ cartRepository
34+ private readonly StoreManagerInterface $ storeManager ,
35+ private readonly QuoteIdMaskFactory $ quoteIdMaskFactory
3936 ) {
4037 }
4138
4239 /**
43- * Before saving a guest cart item, set quote ID from request and validate website assignment
40+ * Update id from request if param cartId exist
4441 *
45- * @param GuestCartItemRepositoryInterface $subject
42+ * @param GuestCartItemRepositoryInterface $guestCartItemRepository
4643 * @param CartItemInterface $cartItem
47- * @return void
48- * @throws LocalizedException
49- *
44+ * @return array
5045 * @SuppressWarnings(PHPMD.UnusedFormalParameter)
5146 */
5247 public function beforeSave (
53- GuestCartItemRepositoryInterface $ subject ,
48+ GuestCartItemRepositoryInterface $ guestCartItemRepository ,
5449 CartItemInterface $ cartItem
55- ): void {
56- if ($ cartId = $ this ->request ->getParam ('cartId ' )) {
50+ ): array {
51+ $ cartId = $ this ->request ->getParam ('cartId ' );
52+
53+ if ($ cartId ) {
5754 $ cartItem ->setQuoteId ($ cartId );
5855 }
59-
6056 $ this ->validateProductWebsiteAssignment ($ cartItem );
57+ return [$ cartItem ];
6158 }
6259
6360 /**
64- * Validate product's website assignment for guest cart item
61+ * Validate that product is assigned to the current website
6562 *
63+ * @param CartItemInterface $cartItem
6664 * @throws LocalizedException
65+ * @throws NoSuchEntityException
6766 */
6867 private function validateProductWebsiteAssignment (CartItemInterface $ cartItem ): void
6968 {
@@ -72,84 +71,29 @@ private function validateProductWebsiteAssignment(CartItemInterface $cartItem):
7271 return ;
7372 }
7473
74+ // Get current website ID from the masked cart ID
7575 $ maskedQuoteId = $ cartItem ->getQuoteId ();
7676 $ quoteIdMask = $ this ->quoteIdMaskFactory ->create ()->load ($ maskedQuoteId , 'masked_id ' );
77- $ quoteId = $ quoteIdMask ->getQuoteId ();
7877
79- if (!$ quoteId ) {
78+ if (!$ quoteIdMask -> getQuoteId () ) {
8079 return ;
8180 }
82-
83- try {
84- $ quote = $ this ->cartRepository ->get ($ quoteId );
85- $ storeId = $ quote ->getStoreId ();
86-
87- foreach ($ quote ->getAllItems () as $ item ) {
88- if ($ sku === $ item ->getSku ()) {
89- $ this ->validateWebsiteAssignment ($ item ->getProductId (), $ storeId );
90- return ;
91- }
92- }
93-
94- // Product not in quote yet
95- $ this ->validateWebsiteAssignmentBySku ($ sku , $ storeId );
96-
97- } catch (NoSuchEntityException ) {
98- throw new LocalizedException (__ ('Product that you are trying to add is not available. ' ));
99- }
100- }
101-
102- /**
103- * Validate by SKU for new items
104- *
105- * @param string $sku
106- * @param int $storeId
107- * @return void
108- * @throws LocalizedException
109- */
110- private function validateWebsiteAssignmentBySku (string $ sku , int $ storeId ): void
111- {
112- try {
113- $ product = $ this ->productRepository ->get ($ sku , false , $ storeId );
114- $ this ->checkProductInWebsite ($ product ->getWebsiteIds (), $ storeId );
115- } catch (NoSuchEntityException ) {
116- throw new LocalizedException (__ ('Product that you are trying to add is not available. ' ));
117- }
118- }
119-
120- /**
121- * Validate by product ID for existing items
122- *
123- * @param int $productId
124- * @param int $storeId
125- * @return void
126- * @throws LocalizedException
127- */
128- private function validateWebsiteAssignment (int $ productId , int $ storeId ): void
129- {
81+ $ currentWebsiteId = $ this ->storeManager ->getStore ()->getWebsiteId ();
13082 try {
131- $ product = $ this ->productRepository ->getById ($ productId , false , $ storeId );
132- $ this ->checkProductInWebsite ($ product ->getWebsiteIds (), $ storeId );
133- } catch (NoSuchEntityException ) {
134- throw new LocalizedException (__ ('Product that you are trying to add is not available. ' ));
135- }
136- }
83+ $ product = $ this ->productRepository ->get ($ sku , false , null );
13784
138- /**
139- * Validate by product ID for existing items
140- *
141- * @param array|null $websiteIds
142- * @param int $storeId
143- * @return void
144- * @throws LocalizedException
145- * @throws NoSuchEntityException
146- */
147- private function checkProductInWebsite (?array $ websiteIds , int $ storeId ): void
148- {
149- $ websiteId = $ this ->storeManager ->getStore ($ storeId )->getWebsiteId ();
85+ $ productWebsiteIds = $ product ->getWebsiteIds ();
15086
151- if (empty ($ websiteIds ) || !in_array ($ websiteId , $ websiteIds , true )) {
152- throw new LocalizedException (__ ('Product that you are trying to add is not available. ' ));
87+ // Validate website assignment
88+ if (!is_array ($ productWebsiteIds ) || !in_array ($ currentWebsiteId , $ productWebsiteIds )) {
89+ throw new LocalizedException (
90+ __ ('Product that you are trying to add is not available. ' )
91+ );
92+ }
93+ } catch (NoSuchEntityException $ e ) {
94+ throw new LocalizedException (
95+ __ ('Product that you are trying to add is not available. ' )
96+ );
15397 }
15498 }
15599}
0 commit comments