9
9
10
10
use Exception ;
11
11
use Magento \Catalog \Api \ProductRepositoryInterface ;
12
+ use Magento \Framework \Exception \LocalizedException ;
12
13
use Magento \Framework \Exception \NoSuchEntityException ;
13
14
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
14
15
use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
15
16
use Magento \Quote \Model \Quote ;
16
17
use Magento \QuoteGraphQl \Model \Cart \BuyRequest \BuyRequestBuilder ;
18
+ use Magento \CatalogInventory \Api \StockStateInterface ;
17
19
18
20
/**
19
21
* Add simple product to cart
@@ -30,16 +32,24 @@ class AddSimpleProductToCart
30
32
*/
31
33
private $ buyRequestBuilder ;
32
34
35
+ /**
36
+ * @var StockStateInterface
37
+ */
38
+ private $ stockState ;
39
+
33
40
/**
34
41
* @param ProductRepositoryInterface $productRepository
35
42
* @param BuyRequestBuilder $buyRequestBuilder
43
+ * @param StockStateInterface $stockState
36
44
*/
37
45
public function __construct (
38
46
ProductRepositoryInterface $ productRepository ,
39
- BuyRequestBuilder $ buyRequestBuilder
47
+ BuyRequestBuilder $ buyRequestBuilder ,
48
+ StockStateInterface $ stockState
40
49
) {
41
50
$ this ->productRepository = $ productRepository ;
42
51
$ this ->buyRequestBuilder = $ buyRequestBuilder ;
52
+ $ this ->stockState = $ stockState ;
43
53
}
44
54
45
55
/**
@@ -53,15 +63,40 @@ public function __construct(
53
63
public function execute (Quote $ cart , array $ cartItemData ): void
54
64
{
55
65
$ sku = $ this ->extractSku ($ cartItemData );
56
-
66
+ $ childSku = $ this ->extractChildSku ($ cartItemData );
67
+ $ childSkuQty = $ this ->extractChildSkuQuantity ($ cartItemData );
57
68
try {
58
69
$ product = $ this ->productRepository ->get ($ sku , false , null , true );
59
70
} catch (NoSuchEntityException $ e ) {
60
71
throw new GraphQlNoSuchEntityException (__ ('Could not find a product with SKU "%sku" ' , ['sku ' => $ sku ]));
61
72
}
62
73
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
+
63
91
try {
64
- $ result = $ cart ->addProduct ($ product , $ this ->buyRequestBuilder ->build ($ cartItemData ));
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
100
} catch (Exception $ e ) {
66
101
throw new GraphQlInputException (
67
102
__ (
@@ -99,4 +134,33 @@ private function extractSku(array $cartItemData): string
99
134
}
100
135
return (string )$ cartItemData ['data ' ]['sku ' ];
101
136
}
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
+ }
102
166
}
0 commit comments