Skip to content

Commit a28ab2f

Browse files
Return product stock data - Code Rewiev FIX
1 parent 98d27f3 commit a28ab2f

File tree

5 files changed

+32
-15
lines changed

5 files changed

+32
-15
lines changed

app/code/Magento/CatalogInventoryGraphQl/Model/Resolver/OnlyXLeftInStockResolver.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
use Magento\Framework\GraphQl\Query\ResolverInterface;
1919
use Magento\Store\Model\ScopeInterface;
2020

21+
/**
22+
* {@inheritdoc}
23+
*/
2124
class OnlyXLeftInStockResolver implements ResolverInterface
2225
{
2326
/**
@@ -51,7 +54,7 @@ public function __construct(
5154
}
5255

5356
/**
54-
* @inheritdoc
57+
* {@inheritdoc}
5558
*/
5659
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null): Value
5760
{
@@ -75,12 +78,22 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
7578
}
7679

7780
/**
81+
* Get product qty left when "Catalog > Inventory > Stock Options > Only X left Threshold" is greater than 0
82+
*
7883
* @param ProductInterface $product
7984
*
8085
* @return null|float
8186
*/
8287
private function getOnlyXLeftQty(ProductInterface $product): ?float
8388
{
89+
$thresholdQty = (float)$this->scopeConfig->getValue(
90+
Configuration::XML_PATH_STOCK_THRESHOLD_QTY,
91+
ScopeInterface::SCOPE_STORE
92+
);
93+
if($thresholdQty === 0){
94+
return null;
95+
}
96+
8497
$stockItem = $this->stockRegistry->getStockItem($product->getId());
8598

8699
$stockCurrentQty = $this->stockRegistry->getStockStatus(

app/code/Magento/CatalogInventoryGraphQl/Model/Resolver/StockStatusProvider.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88
namespace Magento\CatalogInventoryGraphQl\Model\Resolver;
99

1010
use Magento\Catalog\Api\Data\ProductInterface;
11+
use Magento\CatalogInventory\Api\Data\StockStatusInterface;
1112
use Magento\CatalogInventory\Api\StockStatusRepositoryInterface;
1213
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1314
use Magento\Framework\GraphQl\Config\Element\Field;
1415
use Magento\Framework\GraphQl\Query\Resolver\Value;
1516
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
1617
use Magento\Framework\GraphQl\Query\ResolverInterface;
1718

19+
/**
20+
* {@inheritdoc}
21+
*/
1822
class StockStatusProvider implements ResolverInterface
1923
{
2024
/**
@@ -38,7 +42,7 @@ public function __construct(ValueFactory $valueFactory, StockStatusRepositoryInt
3842
}
3943

4044
/**
41-
* @inheritdoc
45+
* {@inheritdoc}
4246
*/
4347
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null): Value
4448
{
@@ -54,9 +58,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
5458
$product = $value['model'];
5559

5660
$stockStatus = $this->stockStatusRepository->get($product->getId());
61+
$productStockStatus = (int)$stockStatus->getStockStatus();
5762

58-
$result = function () use ($stockStatus) {
59-
return $stockStatus->getStockStatus();
63+
$result = function () use ($productStockStatus) {
64+
return $productStockStatus === StockStatusInterface::STATUS_IN_STOCK ? 'IN_STOCK' : 'OUT_OF_STOCK';
6065
};
6166

6267
return $this->valueFactory->create($result);

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@
33

44
interface ProductInterface {
55
only_x_left_in_stock: Float @doc(description: "Product stock only x left count") @resolver(class: "Magento\\CatalogInventoryGraphQl\\Model\\Resolver\\OnlyXLeftInStockResolver")
6-
stock_status: Int @doc(description: "Stock status of the product") @resolver(class: "Magento\\CatalogInventoryGraphQl\\Model\\Resolver\\StockStatusProvider")
6+
stock_status: ProductStockStatus @doc(description: "Stock status of the product") @resolver(class: "Magento\\CatalogInventoryGraphQl\\Model\\Resolver\\StockStatusProvider")
7+
}
8+
9+
enum ProductStockStatus @doc(description: "This enumeration states whether a product stock status is in stock or out of stock") {
10+
IN_STOCK
11+
OUT_OF_STOCK
712
}

app/code/Magento/CatalogInventoryGraphQl/Test/CatalogInventory/ProductOnlyXLeftInStockTest.php renamed to dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogInventory/ProductOnlyXLeftInStockTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\CatalogInventoryGraphQl\Test\CatalogInventory;
8+
namespace Magento\GraphQl\CatalogInventory;
99

1010
use Magento\TestFramework\TestCase\GraphQlAbstract;
1111

app/code/Magento/CatalogInventoryGraphQl/Test/CatalogInventory/ProductStockStatusTest.php renamed to dev/tests/api-functional/testsuite/Magento/GraphQl/CatalogInventory/ProductStockStatusTest.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\CatalogInventoryGraphQl\Test\CatalogInventory;
8+
namespace Magento\GraphQl\CatalogInventory;
99

1010
use Magento\CatalogInventory\Api\Data\StockStatusInterface;
1111
use Magento\CatalogInventory\Api\StockRegistryInterface;
@@ -46,10 +46,7 @@ public function testQueryProductStockStatusInStock()
4646

4747
$this->assertArrayHasKey(0, $response['products']['items']);
4848
$this->assertArrayHasKey('stock_status', $response['products']['items'][0]);
49-
$this->assertEquals(
50-
StockStatusInterface::STATUS_IN_STOCK,
51-
$response['products']['items'][0]['stock_status']
52-
);
49+
$this->assertEquals('IN_STOCK', $response['products']['items'][0]['stock_status']);
5350
}
5451

5552
/**
@@ -79,9 +76,6 @@ public function testQueryProductStockStatusOutOfStock()
7976

8077
$this->assertArrayHasKey(0, $response['products']['items']);
8178
$this->assertArrayHasKey('stock_status', $response['products']['items'][0]);
82-
$this->assertEquals(
83-
StockStatusInterface::STATUS_OUT_OF_STOCK,
84-
$response['products']['items'][0]['stock_status']
85-
);
79+
$this->assertEquals('OUT_OF_STOCK', $response['products']['items'][0]['stock_status']);
8680
}
8781
}

0 commit comments

Comments
 (0)