7
7
8
8
namespace Magento \Quote \Model \Cart ;
9
9
10
+ use Magento \CatalogInventory \Api \StockRegistryInterface ;
10
11
use Magento \Framework \Exception \NoSuchEntityException ;
11
12
use Magento \Quote \Api \CartRepositoryInterface ;
12
13
use Magento \Quote \Model \Cart \BuyRequest \BuyRequestBuilder ;
21
22
*/
22
23
class AddProductsToCart
23
24
{
24
- /**
25
- * @var CartRepositoryInterface
26
- */
27
- private $ cartRepository ;
28
-
29
- /**
30
- * @var MaskedQuoteIdToQuoteIdInterface
31
- */
32
- private $ maskedQuoteIdToQuoteId ;
33
-
34
- /**
35
- * @var BuyRequestBuilder
36
- */
37
- private $ requestBuilder ;
38
-
39
- /**
40
- * @var ProductReaderInterface
41
- */
42
- private $ productReader ;
43
-
44
- /**
45
- * @var AddProductsToCartError
46
- */
47
- private $ error ;
48
-
49
25
/**
50
26
* @param CartRepositoryInterface $cartRepository
51
27
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
52
28
* @param BuyRequestBuilder $requestBuilder
53
29
* @param ProductReaderInterface $productReader
54
- * @param AddProductsToCartError $addProductsToCartError
30
+ * @param AddProductsToCartError $error
31
+ * @param StockRegistryInterface $stockRegistry
55
32
*/
56
33
public function __construct (
57
- CartRepositoryInterface $ cartRepository ,
58
- MaskedQuoteIdToQuoteIdInterface $ maskedQuoteIdToQuoteId ,
59
- BuyRequestBuilder $ requestBuilder ,
60
- ProductReaderInterface $ productReader ,
61
- AddProductsToCartError $ addProductsToCartError
34
+ private readonly CartRepositoryInterface $ cartRepository ,
35
+ private readonly MaskedQuoteIdToQuoteIdInterface $ maskedQuoteIdToQuoteId ,
36
+ private readonly BuyRequestBuilder $ requestBuilder ,
37
+ private readonly ProductReaderInterface $ productReader ,
38
+ private readonly AddProductsToCartError $ error ,
39
+ private readonly StockRegistryInterface $ stockRegistry
62
40
) {
63
- $ this ->cartRepository = $ cartRepository ;
64
- $ this ->maskedQuoteIdToQuoteId = $ maskedQuoteIdToQuoteId ;
65
- $ this ->requestBuilder = $ requestBuilder ;
66
- $ this ->productReader = $ productReader ;
67
- $ this ->error = $ addProductsToCartError ;
68
41
}
69
42
70
43
/**
@@ -128,7 +101,16 @@ function ($item) {
128
101
);
129
102
$ this ->productReader ->loadProducts ($ skus , $ cart ->getStoreId ());
130
103
foreach ($ cartItems as $ cartItemPosition => $ cartItem ) {
131
- $ errors = $ this ->addItemToCart ($ cart , $ cartItem , $ cartItemPosition );
104
+ $ product = $ this ->productReader ->getProductBySku ($ cartItem ->getSku ());
105
+ $ stockItemQuantity = 0.0 ;
106
+ if ($ product ) {
107
+ $ stockItem = $ this ->stockRegistry ->getStockItem (
108
+ $ product ->getId (),
109
+ $ cart ->getStore ()->getWebsiteId ()
110
+ );
111
+ $ stockItemQuantity = $ stockItem ->getQty () - $ stockItem ->getMinQty ();
112
+ }
113
+ $ errors = $ this ->addItemToCart ($ cart , $ cartItem , $ cartItemPosition , $ stockItemQuantity );
132
114
if ($ errors ) {
133
115
$ failedCartItems [$ cartItemPosition ] = $ errors ;
134
116
}
@@ -143,42 +125,53 @@ function ($item) {
143
125
* @param Quote $cart
144
126
* @param Data\CartItem $cartItem
145
127
* @param int $cartItemPosition
128
+ * @param float $stockItemQuantity
146
129
* @return array
147
130
*/
148
- private function addItemToCart (Quote $ cart , Data \CartItem $ cartItem , int $ cartItemPosition ): array
149
- {
131
+ private function addItemToCart (
132
+ Quote $ cart ,
133
+ Data \CartItem $ cartItem ,
134
+ int $ cartItemPosition ,
135
+ float $ stockItemQuantity
136
+ ): array {
150
137
$ sku = $ cartItem ->getSku ();
151
138
$ errors = [];
152
139
$ result = null ;
153
140
154
141
if ($ cartItem ->getQuantity () <= 0 ) {
155
142
$ errors [] = $ this ->error ->create (
156
143
__ ('The product quantity should be greater than 0 ' )->render (),
157
- $ cartItemPosition
144
+ $ cartItemPosition ,
145
+ $ stockItemQuantity
158
146
);
159
- } else {
160
- $ productBySku = $ this ->productReader ->getProductBySku ($ sku );
161
- $ product = isset ($ productBySku ) ? clone $ productBySku : null ;
162
- if (!$ product || !$ product ->isSaleable () || !$ product ->isAvailable ()) {
163
- $ errors [] = $ this ->error ->create (
147
+ }
148
+
149
+ $ productBySku = $ this ->productReader ->getProductBySku ($ sku );
150
+ $ product = isset ($ productBySku ) ? clone $ productBySku : null ;
151
+
152
+ if (!$ product || !$ product ->isSaleable () || !$ product ->isAvailable ()) {
153
+ return [
154
+ $ this ->error ->create (
164
155
__ ('Could not find a product with SKU "%sku" ' , ['sku ' => $ sku ])->render (),
165
- $ cartItemPosition
166
- );
167
- } else {
168
- try {
169
- $ result = $ cart ->addProduct ($ product , $ this ->requestBuilder ->build ($ cartItem ));
170
- } catch (\Throwable $ e ) {
171
- $ errors [] = $ this ->error ->create (
172
- __ ($ e ->getMessage ())->render (),
173
- $ cartItemPosition
174
- );
175
- }
176
- }
156
+ $ cartItemPosition ,
157
+ $ stockItemQuantity
158
+ )
159
+ ];
160
+ }
177
161
178
- if (is_string ($ result )) {
179
- foreach (array_unique (explode ("\n" , $ result )) as $ error ) {
180
- $ errors [] = $ this ->error ->create (__ ($ error )->render (), $ cartItemPosition );
181
- }
162
+ try {
163
+ $ result = $ cart ->addProduct ($ product , $ this ->requestBuilder ->build ($ cartItem ));
164
+ } catch (\Throwable $ e ) {
165
+ $ errors [] = $ this ->error ->create (
166
+ __ ($ e ->getMessage ())->render (),
167
+ $ cartItemPosition ,
168
+ $ stockItemQuantity
169
+ );
170
+ }
171
+
172
+ if (is_string ($ result )) {
173
+ foreach (array_unique (explode ("\n" , $ result )) as $ error ) {
174
+ $ errors [] = $ this ->error ->create (__ ($ error )->render (), $ cartItemPosition , $ stockItemQuantity );
182
175
}
183
176
}
184
177
0 commit comments