99
1010use Exception ;
1111use Magento \Catalog \Api \ProductRepositoryInterface ;
12- use Magento \Framework \Exception \LocalizedException ;
1312use Magento \Framework \Exception \NoSuchEntityException ;
1413use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
1514use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
1615use Magento \Quote \Model \Quote ;
1716use Magento \QuoteGraphQl \Model \Cart \BuyRequest \BuyRequestBuilder ;
18- use Magento \CatalogInventory \Api \StockStateInterface ;
1917
2018/**
2119 * Add simple product to cart
@@ -32,24 +30,16 @@ class AddSimpleProductToCart
3230 */
3331 private $ buyRequestBuilder ;
3432
35- /**
36- * @var StockStateInterface
37- */
38- private $ stockState ;
39-
4033 /**
4134 * @param ProductRepositoryInterface $productRepository
4235 * @param BuyRequestBuilder $buyRequestBuilder
43- * @param StockStateInterface $stockState
4436 */
4537 public function __construct (
4638 ProductRepositoryInterface $ productRepository ,
47- BuyRequestBuilder $ buyRequestBuilder ,
48- StockStateInterface $ stockState
39+ BuyRequestBuilder $ buyRequestBuilder
4940 ) {
5041 $ this ->productRepository = $ productRepository ;
5142 $ this ->buyRequestBuilder = $ buyRequestBuilder ;
52- $ this ->stockState = $ stockState ;
5343 }
5444
5545 /**
@@ -62,41 +52,17 @@ public function __construct(
6252 */
6353 public function execute (Quote $ cart , array $ cartItemData ): void
6454 {
55+ $ cartItemData ['model ' ] = $ cart ;
6556 $ sku = $ this ->extractSku ($ cartItemData );
66- $ childSku = $ this ->extractChildSku ($ cartItemData );
67- $ childSkuQty = $ this ->extractChildSkuQuantity ($ cartItemData );
57+
6858 try {
6959 $ product = $ this ->productRepository ->get ($ sku , false , null , true );
7060 } catch (NoSuchEntityException $ e ) {
7161 throw new GraphQlNoSuchEntityException (__ ('Could not find a product with SKU "%sku" ' , ['sku ' => $ sku ]));
7262 }
7363
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-
9164 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 ));
10066 } catch (Exception $ e ) {
10167 throw new GraphQlInputException (
10268 __ (
@@ -134,33 +100,4 @@ private function extractSku(array $cartItemData): string
134100 }
135101 return (string )$ cartItemData ['data ' ]['sku ' ];
136102 }
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- }
166103}
0 commit comments