88namespace Magento \ConfigurableProductGraphQl \Model \Cart \BuyRequest ;
99
1010use Magento \Catalog \Api \Data \ProductInterface ;
11+ use Magento \Catalog \Api \ProductRepositoryInterface ;
1112use Magento \CatalogInventory \Api \StockStateInterface ;
13+ use Magento \ConfigurableProductGraphQl \Model \Options \Collection as OptionCollection ;
14+ use Magento \Framework \EntityManager \MetadataPool ;
1215use Magento \Framework \Exception \LocalizedException ;
1316use Magento \Framework \Exception \NoSuchEntityException ;
1417use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
1518use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
1619use Magento \Framework \Stdlib \ArrayManager ;
17- use Magento \QuoteGraphQl \Model \Cart \BuyRequest \BuyRequestDataProviderInterface ;
18- use Magento \Catalog \Api \ProductRepositoryInterface ;
19- use Magento \ConfigurableProductGraphQl \Model \Options \Collection as OptionCollection ;
20- use Magento \Framework \EntityManager \MetadataPool ;
2120use Magento \Quote \Model \Quote ;
21+ use Magento \QuoteGraphQl \Model \Cart \BuyRequest \BuyRequestDataProviderInterface ;
2222
2323/**
2424 * DataProvider for building super attribute options in buy requests
@@ -76,7 +76,6 @@ public function __construct(
7676 */
7777 public function execute (array $ cartItemData ): array
7878 {
79-
8079 $ parentSku = $ this ->arrayManager ->get ('parent_sku ' , $ cartItemData );
8180 if ($ parentSku === null ) {
8281 return [];
@@ -94,17 +93,7 @@ public function execute(array $cartItemData): array
9493 throw new GraphQlNoSuchEntityException (__ ('Could not find specified product. ' ));
9594 }
9695
97-
98- // Child stock check has to be performed a catalog by default would not show/check it
99- $ childProduct = $ this ->productRepository ->get ($ sku , false , null , true );
100-
101- $ result = $ this ->stockState ->checkQuoteItemQty ($ childProduct ->getId (), $ qty , $ qty , $ qty , $ cart ->getStoreId ());
102-
103- if ($ result ->getHasError () ) {
104- throw new LocalizedException (
105- __ ($ result ->getMessage ())
106- );
107- }
96+ $ this ->checkProductStock ($ sku , (float ) $ qty , (int ) $ cart ->getStoreId ());
10897
10998 $ configurableProductLinks = $ parentProduct ->getExtensionAttributes ()->getConfigurableProductLinks ();
11099 if (!in_array ($ product ->getId (), $ configurableProductLinks )) {
@@ -124,12 +113,47 @@ public function execute(array $cartItemData): array
124113 }
125114 }
126115 }
127- // Some options might be disabled and/or available when parent and child sku are provided
116+ $ this ->checkSuperAttributeData ($ parentSku , $ superAttributesData );
117+
118+ return ['super_attribute ' => $ superAttributesData ];
119+ }
120+
121+ /**
122+ * Stock check for a product
123+ *
124+ * @param string $sku
125+ * @param float $qty
126+ * @param int $scopeId
127+ */
128+ private function checkProductStock (string $ sku , float $ qty , int $ scopeId ): void
129+ {
130+ // Child stock check has to be performed a catalog by default would not show/check it
131+ $ childProduct = $ this ->productRepository ->get ($ sku , false , null , true );
132+
133+ $ result = $ this ->stockState ->checkQuoteItemQty ($ childProduct ->getId (), $ qty , $ qty , $ qty , $ scopeId );
134+
135+ if ($ result ->getHasError ()) {
136+ throw new LocalizedException (
137+ __ ($ result ->getMessage ())
138+ );
139+ }
140+ }
141+
142+ /**
143+ * Check super attribute data.
144+ *
145+ * Some options might be disabled and/or available when parent and child sku are provided.
146+ *
147+ * @param string $parentSku
148+ * @param array $superAttributesData
149+ * @throws LocalizedException
150+ */
151+ private function checkSuperAttributeData (string $ parentSku , array $ superAttributesData ): void
152+ {
128153 if (empty ($ superAttributesData )) {
129154 throw new LocalizedException (
130155 __ ('The product with SKU %sku is out of stock. ' , ['sku ' => $ parentSku ])
131156 );
132157 }
133- return ['super_attribute ' => $ superAttributesData ];
134158 }
135159}
0 commit comments