9
9
10
10
use Exception ;
11
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
12
- use Magento \Framework \Exception \LocalizedException ;
13
12
use Magento \Framework \Exception \NoSuchEntityException ;
14
13
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
15
14
use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
16
15
use Magento \Quote \Model \Quote ;
17
16
use Magento \QuoteGraphQl \Model \Cart \BuyRequest \BuyRequestBuilder ;
18
- use Magento \CatalogInventory \Api \StockStateInterface ;
19
17
20
18
/**
21
19
* Add simple product to cart
@@ -32,24 +30,16 @@ class AddSimpleProductToCart
32
30
*/
33
31
private $ buyRequestBuilder ;
34
32
35
- /**
36
- * @var StockStateInterface
37
- */
38
- private $ stockState ;
39
-
40
33
/**
41
34
* @param ProductRepositoryInterface $productRepository
42
35
* @param BuyRequestBuilder $buyRequestBuilder
43
- * @param StockStateInterface $stockState
44
36
*/
45
37
public function __construct (
46
38
ProductRepositoryInterface $ productRepository ,
47
- BuyRequestBuilder $ buyRequestBuilder ,
48
- StockStateInterface $ stockState
39
+ BuyRequestBuilder $ buyRequestBuilder
49
40
) {
50
41
$ this ->productRepository = $ productRepository ;
51
42
$ this ->buyRequestBuilder = $ buyRequestBuilder ;
52
- $ this ->stockState = $ stockState ;
53
43
}
54
44
55
45
/**
@@ -62,41 +52,17 @@ public function __construct(
62
52
*/
63
53
public function execute (Quote $ cart , array $ cartItemData ): void
64
54
{
55
+ $ cartItemData ['model ' ] = $ cart ;
65
56
$ sku = $ this ->extractSku ($ cartItemData );
66
- $ childSku = $ this ->extractChildSku ($ cartItemData );
67
- $ childSkuQty = $ this ->extractChildSkuQuantity ($ cartItemData );
57
+
68
58
try {
69
59
$ product = $ this ->productRepository ->get ($ sku , false , null , true );
70
60
} catch (NoSuchEntityException $ e ) {
71
61
throw new GraphQlNoSuchEntityException (__ ('Could not find a product with SKU "%sku" ' , ['sku ' => $ sku ]));
72
62
}
73
63
74
- if ($ childSku ) {
75
- $ childProduct = $ this ->productRepository ->get ($ childSku , false , null , true );
76
-
77
- $ result = $ this ->stockState ->checkQuoteItemQty (
78
- $ childProduct ->getId (), $ childSkuQty , $ childSkuQty , $ childSkuQty , $ cart ->getStoreId ()
79
- );
80
-
81
- if ($ result ->getHasError () ) {
82
- throw new GraphQlInputException (
83
- __ (
84
- 'Could not add the product with SKU %sku to the shopping cart: %message ' ,
85
- ['sku ' => $ childSku , 'message ' => __ ($ result ->getMessage ())]
86
- )
87
- );
88
- }
89
- }
90
-
91
64
try {
92
- $ buyRequest = $ this ->buyRequestBuilder ->build ($ cartItemData );
93
- // Some options might be disabled and not available
94
- if (empty ($ buyRequest ['super_attribute ' ])) {
95
- throw new LocalizedException (
96
- __ ('The product with SKU %sku is out of stock. ' , ['sku ' => $ childSku ])
97
- );
98
- }
99
- $ result = $ cart ->addProduct ($ product , $ this ->buyRequestBuilder ->build ($ cartItemData ));
65
+ $ result = $ cart ->addProduct ($ product , $ this ->buyRequestBuilder ->build ($ cartItemData ));
100
66
} catch (Exception $ e ) {
101
67
throw new GraphQlInputException (
102
68
__ (
@@ -134,33 +100,4 @@ private function extractSku(array $cartItemData): string
134
100
}
135
101
return (string )$ cartItemData ['data ' ]['sku ' ];
136
102
}
137
-
138
- /**
139
- * Extract option child SKU from cart item data
140
- *
141
- * @param array $cartItemData
142
- * @return string
143
- * @throws GraphQlInputException
144
- */
145
- private function extractChildSku (array $ cartItemData ): ?string
146
- {
147
- if (isset ($ cartItemData ['data ' ]['sku ' ])) {
148
- return (string )$ cartItemData ['data ' ]['sku ' ];
149
- }
150
- }
151
-
152
- /**
153
- * Extract option child SKU from cart item data
154
- *
155
- * @param array $cartItemData
156
- * @return string
157
- * @throws GraphQlInputException
158
- */
159
- private function extractChildSkuQuantity (array $ cartItemData ): ?string
160
- {
161
- if (empty ($ cartItemData ['data ' ]['quantity ' ])) {
162
- throw new GraphQlInputException (__ ('Missed "quantity" in cart item data ' ));
163
- }
164
- return (string )$ cartItemData ['data ' ]['quantity ' ];
165
- }
166
103
}
0 commit comments