Skip to content

Commit 1438c4c

Browse files
bl4desvera
authored andcommitted
is_available attribute in CartItemInterface returns true even when salable stock is lower than the quantity of the product (#231)
1 parent 3c8ae28 commit 1438c4c

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

app/code/Magento/QuoteGraphQl/Model/CartItem/ProductStock.php

+25-28
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
use Magento\Catalog\Api\Data\ProductInterface;
1111
use Magento\Catalog\Api\ProductRepositoryInterface;
12-
use Magento\CatalogInventory\Api\StockStatusRepositoryInterface;
1312
use Magento\Framework\Exception\NoSuchEntityException;
1413
use Magento\Quote\Model\Quote\Item;
1514

@@ -28,20 +27,13 @@ class ProductStock
2827
*/
2928
private const PRODUCT_TYPE_CONFIGURABLE = "configurable";
3029

31-
/**
32-
* Simple product type code
33-
*/
34-
private const PRODUCT_TYPE_SIMPLE = "simple";
35-
3630
/**
3731
* ProductStock constructor
3832
*
39-
* @param StockStatusRepositoryInterface $stockStatusRepository
4033
* @param ProductRepositoryInterface $productRepositoryInterface
4134
*/
4235
public function __construct(
43-
private readonly StockStatusRepositoryInterface $stockStatusRepository,
44-
private readonly ProductRepositoryInterface $productRepositoryInterface
36+
private readonly ProductRepositoryInterface $productRepositoryInterface,
4537
) {
4638
}
4739

@@ -57,7 +49,7 @@ public function isProductAvailable(Item $cartItem): bool
5749
$requestedQty = 0;
5850
$previousQty = 0;
5951
/**
60-
* @var ProductInterface $variantProduct
52+
* @var ProductInterface $variantProduct
6153
* Configurable products cannot have stock, only its variants can. If the user adds a configurable product
6254
* using its SKU and the selected options, we need to get the variant it refers to from the quote.
6355
*/
@@ -81,24 +73,10 @@ public function isProductAvailable(Item $cartItem): bool
8173
}
8274

8375
$requiredItemQty = $requestedQty + $previousQty;
84-
$productId = (int) $cartItem->getProduct()->getId();
8576
if ($variantProduct !== null) {
86-
$productId = (int)$variantProduct->getId();
77+
return $this->isStockQtyAvailable($variantProduct, $requiredItemQty);
8778
}
88-
return $this->isStockAvailable($productId, $requiredItemQty);
89-
}
90-
91-
/**
92-
* Check if is required product available in stock
93-
*
94-
* @param int $productId
95-
* @param float $requiredQuantity
96-
* @return bool
97-
*/
98-
private function isStockAvailable(int $productId, float $requiredQuantity): bool
99-
{
100-
$stock = $this->stockStatusRepository->get($productId);
101-
return $stock->getQty() >= $requiredQuantity;
79+
return $this->isStockQtyAvailable($cartItem->getProduct(), $requiredItemQty);
10280
}
10381

10482
/**
@@ -114,15 +92,34 @@ public function isStockAvailableBundle(Item $cartItem, int $previousQty, $reques
11492
$qtyOptions = $cartItem->getQtyOptions();
11593
$totalRequestedQty = $previousQty + $requestedQty;
11694
foreach ($qtyOptions as $qtyOption) {
117-
$productId = (int)$qtyOption->getProductId();
11895
$requiredItemQty = $qtyOption->getValue();
11996
if ($totalRequestedQty) {
12097
$requiredItemQty = $requiredItemQty * $totalRequestedQty;
12198
}
122-
if (!$this->isStockAvailable($productId, $requiredItemQty)) {
99+
if (!$this->isStockQtyAvailable($qtyOption->getProduct(), $requiredItemQty)) {
123100
return false;
124101
}
125102
}
126103
return true;
127104
}
105+
106+
/**
107+
* Check if product is available in stock using quantity from Catalog Inventory Stock Item
108+
*
109+
* @param ProductInterface $product
110+
* @param float $requiredQuantity
111+
* @throws NoSuchEntityException
112+
* @return bool
113+
*/
114+
private function isStockQtyAvailable(ProductInterface $product, float $requiredQuantity): bool
115+
{
116+
$stockItem = $product->getExtensionAttributes()->getStockItem();
117+
if ($stockItem === null) {
118+
return true;
119+
}
120+
if ((int) $stockItem->getProductId() !== (int) $product->getId()) {
121+
throw new NoSuchEntityException(__('Stock item\'s product ID does not match requested product ID'));
122+
}
123+
return $stockItem->getQty() >= $requiredQuantity;
124+
}
128125
}

0 commit comments

Comments
 (0)