8
8
namespace Magento \ConfigurableProductGraphQl \Model \Cart \BuyRequest ;
9
9
10
10
use Magento \Catalog \Api \Data \ProductInterface ;
11
+ use Magento \Catalog \Api \ProductRepositoryInterface ;
11
12
use Magento \CatalogInventory \Api \StockStateInterface ;
13
+ use Magento \ConfigurableProductGraphQl \Model \Options \Collection as OptionCollection ;
14
+ use Magento \Framework \EntityManager \MetadataPool ;
12
15
use Magento \Framework \Exception \LocalizedException ;
13
16
use Magento \Framework \Exception \NoSuchEntityException ;
14
17
use Magento \Framework \GraphQl \Exception \GraphQlInputException ;
15
18
use Magento \Framework \GraphQl \Exception \GraphQlNoSuchEntityException ;
16
19
use 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 ;
21
20
use Magento \Quote \Model \Quote ;
21
+ use Magento \QuoteGraphQl \Model \Cart \BuyRequest \BuyRequestDataProviderInterface ;
22
22
23
23
/**
24
24
* DataProvider for building super attribute options in buy requests
@@ -76,7 +76,6 @@ public function __construct(
76
76
*/
77
77
public function execute (array $ cartItemData ): array
78
78
{
79
-
80
79
$ parentSku = $ this ->arrayManager ->get ('parent_sku ' , $ cartItemData );
81
80
if ($ parentSku === null ) {
82
81
return [];
@@ -94,17 +93,7 @@ public function execute(array $cartItemData): array
94
93
throw new GraphQlNoSuchEntityException (__ ('Could not find specified product. ' ));
95
94
}
96
95
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 ());
108
97
109
98
$ configurableProductLinks = $ parentProduct ->getExtensionAttributes ()->getConfigurableProductLinks ();
110
99
if (!in_array ($ product ->getId (), $ configurableProductLinks )) {
@@ -124,12 +113,47 @@ public function execute(array $cartItemData): array
124
113
}
125
114
}
126
115
}
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
+ {
128
153
if (empty ($ superAttributesData )) {
129
154
throw new LocalizedException (
130
155
__ ('The product with SKU %sku is out of stock. ' , ['sku ' => $ parentSku ])
131
156
);
132
157
}
133
- return ['super_attribute ' => $ superAttributesData ];
134
158
}
135
159
}
0 commit comments