99
1010use Exception ;
1111use Magento \Catalog \Api \ProductRepositoryInterface ;
12+ use Magento \Framework \Exception \LocalizedException ;
1213use Magento \Framework \Exception \NoSuchEntityException ;
1314use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
1415use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
1516use Magento \Quote \Model \Quote ;
1617use Magento \QuoteGraphQl \Model \Cart \BuyRequest \BuyRequestBuilder ;
18+ use Magento \CatalogInventory \Api \StockStateInterface ;
1719
1820/**
1921 * Add simple product to cart
@@ -30,16 +32,24 @@ class AddSimpleProductToCart
3032 */
3133 private $ buyRequestBuilder ;
3234
35+ /**
36+ * @var StockStateInterface
37+ */
38+ private $ stockState ;
39+
3340 /**
3441 * @param ProductRepositoryInterface $productRepository
3542 * @param BuyRequestBuilder $buyRequestBuilder
43+ * @param StockStateInterface $stockState
3644 */
3745 public function __construct (
3846 ProductRepositoryInterface $ productRepository ,
39- BuyRequestBuilder $ buyRequestBuilder
47+ BuyRequestBuilder $ buyRequestBuilder ,
48+ StockStateInterface $ stockState
4049 ) {
4150 $ this ->productRepository = $ productRepository ;
4251 $ this ->buyRequestBuilder = $ buyRequestBuilder ;
52+ $ this ->stockState = $ stockState ;
4353 }
4454
4555 /**
@@ -53,15 +63,40 @@ public function __construct(
5363 public function execute (Quote $ cart , array $ cartItemData ): void
5464 {
5565 $ sku = $ this ->extractSku ($ cartItemData );
56-
66+ $ childSku = $ this ->extractChildSku ($ cartItemData );
67+ $ childSkuQty = $ this ->extractChildSkuQuantity ($ cartItemData );
5768 try {
5869 $ product = $ this ->productRepository ->get ($ sku , false , null , true );
5970 } catch (NoSuchEntityException $ e ) {
6071 throw new GraphQlNoSuchEntityException (__ ('Could not find a product with SKU "%sku" ' , ['sku ' => $ sku ]));
6172 }
6273
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+
6391 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 ));
65100 } catch (Exception $ e ) {
66101 throw new GraphQlInputException (
67102 __ (
@@ -99,4 +134,33 @@ private function extractSku(array $cartItemData): string
99134 }
100135 return (string )$ cartItemData ['data ' ]['sku ' ];
101136 }
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+ }
102166}
0 commit comments