Skip to content

Commit 2a68e71

Browse files
committed
29251 test web-api test fix
1 parent c3e8929 commit 2a68e71

File tree

2 files changed

+41
-69
lines changed

2 files changed

+41
-69
lines changed

app/code/Magento/ConfigurableProductGraphQl/Model/Cart/BuyRequest/SuperAttributeDataProvider.php

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
namespace Magento\ConfigurableProductGraphQl\Model\Cart\BuyRequest;
99

1010
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\CatalogInventory\Api\StockStateInterface;
12+
use Magento\Framework\Exception\LocalizedException;
1113
use Magento\Framework\Exception\NoSuchEntityException;
1214
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1315
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
@@ -16,6 +18,7 @@
1618
use Magento\Catalog\Api\ProductRepositoryInterface;
1719
use Magento\ConfigurableProductGraphQl\Model\Options\Collection as OptionCollection;
1820
use Magento\Framework\EntityManager\MetadataPool;
21+
use Magento\Quote\Model\Quote;
1922

2023
/**
2124
* DataProvider for building super attribute options in buy requests
@@ -42,41 +45,67 @@ class SuperAttributeDataProvider implements BuyRequestDataProviderInterface
4245
*/
4346
private $metadataPool;
4447

48+
/**
49+
* @var StockStateInterface
50+
*/
51+
private $stockState;
52+
4553
/**
4654
* @param ArrayManager $arrayManager
4755
* @param ProductRepositoryInterface $productRepository
4856
* @param OptionCollection $optionCollection
4957
* @param MetadataPool $metadataPool
58+
* @param StockStateInterface $stockState
5059
*/
5160
public function __construct(
5261
ArrayManager $arrayManager,
5362
ProductRepositoryInterface $productRepository,
5463
OptionCollection $optionCollection,
55-
MetadataPool $metadataPool
64+
MetadataPool $metadataPool,
65+
StockStateInterface $stockState
5666
) {
5767
$this->arrayManager = $arrayManager;
5868
$this->productRepository = $productRepository;
5969
$this->optionCollection = $optionCollection;
6070
$this->metadataPool = $metadataPool;
71+
$this->stockState = $stockState;
6172
}
6273

6374
/**
6475
* @inheritdoc
6576
*/
6677
public function execute(array $cartItemData): array
6778
{
79+
6880
$parentSku = $this->arrayManager->get('parent_sku', $cartItemData);
6981
if ($parentSku === null) {
7082
return [];
7183
}
7284
$sku = $this->arrayManager->get('data/sku', $cartItemData);
73-
85+
$qty = $this->arrayManager->get('data/quantity', $cartItemData);
86+
$cart = $this->arrayManager->get('model', $cartItemData);
87+
if (!$cart instanceof Quote) {
88+
throw new LocalizedException(__('"model" value should be specified'));
89+
}
7490
try {
7591
$parentProduct = $this->productRepository->get($parentSku);
7692
$product = $this->productRepository->get($sku);
7793
} catch (NoSuchEntityException $e) {
7894
throw new GraphQlNoSuchEntityException(__('Could not find specified product.'));
7995
}
96+
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+
}
108+
80109
$configurableProductLinks = $parentProduct->getExtensionAttributes()->getConfigurableProductLinks();
81110
if (!in_array($product->getId(), $configurableProductLinks)) {
82111
throw new GraphQlInputException(__('Could not find specified product.'));
@@ -95,6 +124,12 @@ public function execute(array $cartItemData): array
95124
}
96125
}
97126
}
127+
// Some options might be disabled and/or available when parent and child sku are provided
128+
if (empty($superAttributesData)) {
129+
throw new LocalizedException(
130+
__('The product with SKU %sku is out of stock.', ['sku' => $parentSku])
131+
);
132+
}
98133
return ['super_attribute' => $superAttributesData];
99134
}
100135
}

app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99

1010
use Exception;
1111
use Magento\Catalog\Api\ProductRepositoryInterface;
12-
use Magento\Framework\Exception\LocalizedException;
1312
use Magento\Framework\Exception\NoSuchEntityException;
1413
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1514
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1615
use Magento\Quote\Model\Quote;
1716
use 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

Comments
 (0)