Skip to content

Commit 4e89e48

Browse files
committed
Create new ProductStock class to check availability of product
1 parent 1e6c822 commit 4e89e48

File tree

4 files changed

+126
-51
lines changed

4 files changed

+126
-51
lines changed

app/code/Magento/QuoteGraphQl/Model/Resolver/CheckAvailability.php renamed to app/code/Magento/QuoteGraphQl/Model/CartItem/ProductStock.php

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,15 @@
1616
*/
1717
declare(strict_types=1);
1818

19-
namespace Magento\QuoteGraphQl\Model\Resolver;
19+
namespace Magento\QuoteGraphQl\Model\CartItem;
2020

21-
use Magento\Framework\Exception\LocalizedException;
22-
use Magento\Framework\GraphQl\Config\Element\Field;
23-
use Magento\Framework\GraphQl\Query\ResolverInterface;
24-
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
2521
use Magento\CatalogInventory\Api\StockStatusRepositoryInterface;
2622
use Magento\Quote\Model\Quote\Item;
2723

2824
/**
29-
* @inheritdoc
25+
* Product Stock class to check availability of product
3026
*/
31-
class CheckAvailability implements ResolverInterface
27+
class ProductStock
3228
{
3329
/**
3430
* Product type code
@@ -38,10 +34,10 @@ class CheckAvailability implements ResolverInterface
3834
/**
3935
* @var StockStatusRepositoryInterface
4036
*/
41-
private StockStatusRepositoryInterface $stockStatusRepository;
37+
private $stockStatusRepository;
4238

4339
/**
44-
* CheckAvailability constructor
40+
* ProductStock constructor
4541
*
4642
* @param StockStatusRepositoryInterface $stockStatusRepository
4743
*/
@@ -51,35 +47,17 @@ public function __construct(
5147
$this->stockStatusRepository = $stockStatusRepository;
5248
}
5349

54-
/**
55-
* @inheritdoc
56-
*/
57-
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
58-
{
59-
if (!isset($value['model'])) {
60-
throw new LocalizedException(__('"model" value should be specified'));
61-
}
62-
/** @var Item $cartItem */
63-
$cartItem = $value['model'];
64-
65-
return $this->checkProductQtyStatus($cartItem) ? "available" : "unavailable";
66-
}
67-
6850
/**
6951
* Check item status available or unavailable
7052
*
7153
* @param Item $cartItem
7254
* @return bool
7355
*/
74-
private function checkProductQtyStatus(Item $cartItem): bool
56+
public function getProductAvailability($cartItem):bool
7557
{
7658
$requestedQty = 0;
7759
$previousQty = 0;
7860

79-
if (!$cartItem->getQuote()->getItems()) {
80-
return false;
81-
}
82-
8361
foreach ($cartItem->getQuote()->getItems() as $item) {
8462
if ($item->getItemId() == $cartItem->getItemId()) {
8563
$requestedQty = $item->getQtyToAdd() ?? $item->getQty();
@@ -96,14 +74,16 @@ private function checkProductQtyStatus(Item $cartItem): bool
9674
if ($totalRequestedQty) {
9775
$requiredItemQty = $requiredItemQty * $totalRequestedQty;
9876
}
99-
if (!$this->isRequiredStockAvailable($productId, $requiredItemQty)) {
77+
if ($this->isStockAvailable($productId, $requiredItemQty)) {
10078
return false;
10179
}
10280
}
10381
} else {
10482
$requiredItemQty = $requestedQty + $previousQty;
10583
$productId = (int) $cartItem->getProduct()->getId();
106-
return $this->isRequiredStockAvailable($productId, $requiredItemQty);
84+
if ($this->isStockAvailable($productId, $requiredItemQty)) {
85+
return false;
86+
}
10787
}
10888
return true;
10989
}
@@ -115,9 +95,9 @@ private function checkProductQtyStatus(Item $cartItem): bool
11595
* @param float $requiredQuantity
11696
* @return bool
11797
*/
118-
private function isRequiredStockAvailable(int $productId, float $requiredQuantity): bool
98+
private function isStockAvailable(int $productId, float $requiredQuantity): bool
11999
{
120100
$stock = $this->stockStatusRepository->get($productId);
121-
return ($stock->getQty() >= $requiredQuantity);
101+
return ($stock->getQty() < $requiredQuantity) ? true : false;
122102
}
123103
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/************************************************************************
3+
*
4+
* Copyright 2023 Adobe
5+
* All Rights Reserved.
6+
*
7+
* NOTICE: All information contained herein is, and remains
8+
* the property of Adobe and its suppliers, if any. The intellectual
9+
* and technical concepts contained herein are proprietary to Adobe
10+
* and its suppliers and are protected by all applicable intellectual
11+
* property laws, including trade secret and copyright laws.
12+
* Dissemination of this information or reproduction of this material
13+
* is strictly forbidden unless prior written permission is obtained
14+
* from Adobe.
15+
* ************************************************************************
16+
*/
17+
declare(strict_types=1);
18+
19+
namespace Magento\QuoteGraphQl\Model\Resolver;
20+
21+
use Magento\Framework\Exception\LocalizedException;
22+
use Magento\Framework\GraphQl\Config\Element\Field;
23+
use Magento\Framework\GraphQl\Query\ResolverInterface;
24+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
25+
use Magento\QuoteGraphQl\Model\CartItem\ProductStock;
26+
use Magento\Quote\Model\Quote\Item;
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
class CheckProductStockAvailability implements ResolverInterface
32+
{
33+
/**
34+
* @var ProductStock
35+
*/
36+
private ProductStock $productStock;
37+
38+
/**
39+
* CheckProductStockAvailability constructor
40+
*
41+
* @param ProductStock $productStock
42+
*/
43+
public function __construct(
44+
ProductStock $productStock
45+
) {
46+
$this->productStock = $productStock;
47+
}
48+
49+
/**
50+
* @inheritdoc
51+
*/
52+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
53+
{
54+
if (!isset($value['model'])) {
55+
throw new LocalizedException(__('"model" value should be specified'));
56+
}
57+
/** @var Item $cartItem */
58+
$cartItem = $value['model'];
59+
60+
return $this->productStock->getProductAvailability($cartItem) ? "available" : "unavailable";
61+
}
62+
}

app/code/Magento/QuoteGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ interface CartItemInterface @typeResolver(class: "Magento\\QuoteGraphQl\\Model\\
360360
id: String! @deprecated(reason: "Use `uid` instead.")
361361
uid: ID! @doc(description: "The unique ID for a `CartItemInterface` object.")
362362
quantity: Float! @doc(description: "The quantity of this item in the cart.")
363-
status: String @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CheckAvailability") @doc(description: "If qty is more than stock display status as unavailable else available.")
363+
stock_availability: String @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CheckProductStockAvailability") @doc(description: "If qty is more than stock display status as unavailable else available.")
364364
prices: CartItemPrices @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartItemPrices") @doc(description: "Contains details about the price of the item, including taxes and discounts.")
365365
product: ProductInterface! @doc(description: "Details about an item in the cart.")
366366
errors: [CartItemError!] @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartItemErrors") @doc(description: "An array of errors encountered while loading the cart item")

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/StockStatusTest.php

Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function testStockStatusUnavailableSimpleProduct(): void
8686
$response = $this->graphQlMutation($query);
8787
$responseDataObject = new DataObject($response);
8888

89-
self::assertEquals('unavailable', $responseDataObject->getData('cart/items/0/status'));
89+
self::assertEquals('unavailable', $responseDataObject->getData('cart/items/0/stock_availability'));
9090
}
9191

9292
#[
@@ -102,12 +102,21 @@ public function testStockStatusUnavailableAddSimpleProduct(): void
102102
$response = $this->graphQlMutation($query);
103103
$responseDataObject = new DataObject($response);
104104

105-
self::assertEquals('unavailable', $responseDataObject->getData('addProductsToCart/cart/items/0/status'));
105+
self::assertEquals(
106+
'unavailable',
107+
$responseDataObject->getData('addProductsToCart/cart/items/0/stock_availability')
108+
);
106109
}
107110

108111
#[
109112
DataFixture(ProductFixture::class, ['price' => 100.00], as: 'product'),
110-
DataFixture(BundleSelectionFixture::class, ['sku' => '$product.sku$', 'price' => 100, 'price_type' => 0], as:'link'),
113+
DataFixture(
114+
BundleSelectionFixture::class,
115+
[
116+
'sku' => '$product.sku$', 'price' => 100, 'price_type' => 0
117+
],
118+
as:'link'
119+
),
111120
DataFixture(BundleOptionFixture::class, ['title' => 'Checkbox Options', 'type' => 'checkbox',
112121
'required' => 1,'product_links' => ['$link$']], 'option'),
113122
DataFixture(
@@ -135,12 +144,21 @@ public function testStockStatusUnavailableBundleProduct(): void
135144
$response = $this->graphQlMutation($query);
136145
$responseDataObject = new DataObject($response);
137146

138-
self::assertEquals('unavailable', $responseDataObject->getData('cart/items/0/status'));
147+
self::assertEquals(
148+
'unavailable',
149+
$responseDataObject->getData('cart/items/0/stock_availability')
150+
);
139151
}
140152

141153
#[
142154
DataFixture(ProductFixture::class, ['price' => 100.00], as: 'product'),
143-
DataFixture(BundleSelectionFixture::class, ['sku' => '$product.sku$', 'price' => 100, 'price_type' => 0], as:'link'),
155+
DataFixture(
156+
BundleSelectionFixture::class,
157+
[
158+
'sku' => '$product.sku$', 'price' => 100, 'price_type' => 0
159+
],
160+
as:'link'
161+
),
144162
DataFixture(BundleOptionFixture::class, ['title' => 'Checkbox Options', 'type' => 'checkbox',
145163
'required' => 1,'product_links' => ['$link$']], 'option'),
146164
DataFixture(
@@ -181,7 +199,10 @@ public function testStockStatusUnavailableAddBundleProduct(): void
181199
$response = $this->graphQlMutation($query);
182200
$responseDataObject = new DataObject($response);
183201

184-
self::assertEquals('unavailable', $responseDataObject->getData('addProductsToCart/cart/items/0/status'));
202+
self::assertEquals(
203+
'unavailable',
204+
$responseDataObject->getData('addProductsToCart/cart/items/0/stock_availability')
205+
);
185206
}
186207

187208
#[
@@ -212,7 +233,10 @@ public function testStockStatusUnavailableConfigurableProduct(): void
212233
$response = $this->graphQlMutation($query);
213234
$responseDataObject = new DataObject($response);
214235

215-
self::assertEquals('unavailable', $responseDataObject->getData('cart/items/0/status'));
236+
self::assertEquals(
237+
'unavailable',
238+
$responseDataObject->getData('cart/items/0/stock_availability')
239+
);
216240
}
217241

218242
#[
@@ -242,7 +266,10 @@ public function testStockStatusUnavailableAddConfigurableProduct(): void
242266
$response = $this->graphQlMutation($query);
243267
$responseDataObject = new DataObject($response);
244268

245-
self::assertEquals('unavailable', $responseDataObject->getData('addProductsToCart/cart/items/0/status'));
269+
self::assertEquals(
270+
'unavailable',
271+
$responseDataObject->getData('addProductsToCart/cart/items/0/stock_availability')
272+
);
246273
}
247274

248275
/**
@@ -255,7 +282,7 @@ private function getQuery(string $cartId): string
255282
{
256283
cart(cart_id:"{$cartId}"){
257284
items{
258-
status
285+
stock_availability
259286
}
260287
}
261288
}
@@ -277,16 +304,20 @@ private function mutationAddSimpleProduct(string $cartId, string $sku, int $qty
277304
) {
278305
cart {
279306
items {
280-
status
307+
stock_availability
281308
}
282309
}
283310
}
284311
}
285312
QUERY;
286313
}
287314

288-
private function mutationAddBundleProduct(string $cartId, string $sku, string $bundleOptionIdV2, int $qty = 1): string
289-
{
315+
private function mutationAddBundleProduct(
316+
string $cartId,
317+
string $sku,
318+
string $bundleOptionIdV2,
319+
int $qty = 1
320+
): string {
290321
return <<<QUERY
291322
mutation {
292323
addProductsToCart(
@@ -302,7 +333,7 @@ private function mutationAddBundleProduct(string $cartId, string $sku, string $b
302333
) {
303334
cart {
304335
items {
305-
status
336+
stock_availability
306337
product {
307338
sku
308339
}
@@ -313,8 +344,12 @@ private function mutationAddBundleProduct(string $cartId, string $sku, string $b
313344
QUERY;
314345
}
315346

316-
private function mutationAddConfigurableProduct(string $cartId, string $sku, string $parentSku, int $qty = 1): string
317-
{
347+
private function mutationAddConfigurableProduct(
348+
string $cartId,
349+
string $sku,
350+
string $parentSku,
351+
int $qty = 1
352+
): string {
318353
return <<<QUERY
319354
mutation {
320355
addProductsToCart(
@@ -328,7 +363,7 @@ private function mutationAddConfigurableProduct(string $cartId, string $sku, str
328363
) {
329364
cart {
330365
items {
331-
status
366+
stock_availability
332367
product {
333368
sku
334369
}
@@ -344,5 +379,3 @@ private function generateBundleOptionIdV2(int $optionId, int $selectionId, int $
344379
return base64_encode("bundle/$optionId/$selectionId/$quantity");
345380
}
346381
}
347-
348-

0 commit comments

Comments
 (0)