12
12
use Magento \Framework \Exception \LocalizedException ;
13
13
use Magento \Framework \Exception \NoSuchEntityException ;
14
14
use Magento \Framework \Webapi \Rest \Request as RestRequest ;
15
- use Magento \Quote \Api \CartRepositoryInterface ;
16
15
use Magento \Quote \Api \Data \CartItemInterface ;
17
16
use Magento \Quote \Api \GuestCartItemRepositoryInterface ;
18
17
use Magento \Quote \Model \QuoteIdMaskFactory ;
19
18
use Magento \Store \Model \StoreManagerInterface ;
20
19
21
20
/**
22
- * Plugin to update cart ID from request and validate product website assignment
21
+ * Update cart id from request param
23
22
*/
24
23
class UpdateCartId
25
24
{
@@ -28,42 +27,42 @@ class UpdateCartId
28
27
* @param ProductRepositoryInterface $productRepository
29
28
* @param StoreManagerInterface $storeManager
30
29
* @param QuoteIdMaskFactory $quoteIdMaskFactory
31
- * @param CartRepositoryInterface $cartRepository
32
30
*/
33
31
public function __construct (
34
32
private readonly RestRequest $ request ,
35
33
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
39
36
) {
40
37
}
41
38
42
39
/**
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
44
41
*
45
- * @param GuestCartItemRepositoryInterface $subject
42
+ * @param GuestCartItemRepositoryInterface $guestCartItemRepository
46
43
* @param CartItemInterface $cartItem
47
- * @return void
48
- * @throws LocalizedException
49
- *
44
+ * @return array
50
45
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
51
46
*/
52
47
public function beforeSave (
53
- GuestCartItemRepositoryInterface $ subject ,
48
+ GuestCartItemRepositoryInterface $ guestCartItemRepository ,
54
49
CartItemInterface $ cartItem
55
- ): void {
56
- if ($ cartId = $ this ->request ->getParam ('cartId ' )) {
50
+ ): array {
51
+ $ cartId = $ this ->request ->getParam ('cartId ' );
52
+
53
+ if ($ cartId ) {
57
54
$ cartItem ->setQuoteId ($ cartId );
58
55
}
59
-
60
56
$ this ->validateProductWebsiteAssignment ($ cartItem );
57
+ return [$ cartItem ];
61
58
}
62
59
63
60
/**
64
- * Validate product's website assignment for guest cart item
61
+ * Validate that product is assigned to the current website
65
62
*
63
+ * @param CartItemInterface $cartItem
66
64
* @throws LocalizedException
65
+ * @throws NoSuchEntityException
67
66
*/
68
67
private function validateProductWebsiteAssignment (CartItemInterface $ cartItem ): void
69
68
{
@@ -72,84 +71,29 @@ private function validateProductWebsiteAssignment(CartItemInterface $cartItem):
72
71
return ;
73
72
}
74
73
74
+ // Get current website ID from the masked cart ID
75
75
$ maskedQuoteId = $ cartItem ->getQuoteId ();
76
76
$ quoteIdMask = $ this ->quoteIdMaskFactory ->create ()->load ($ maskedQuoteId , 'masked_id ' );
77
- $ quoteId = $ quoteIdMask ->getQuoteId ();
78
77
79
- if (!$ quoteId ) {
78
+ if (!$ quoteIdMask -> getQuoteId () ) {
80
79
return ;
81
80
}
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 ();
130
82
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 );
137
84
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 ();
150
86
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
+ );
153
97
}
154
98
}
155
99
}
0 commit comments