Skip to content

Commit

Permalink
Merge pull request magento#1229 from magento-engcom/issue-1227
Browse files Browse the repository at this point in the history
1227: Fix is getSalableQty for Out of Stock products
  • Loading branch information
maghamed authored Jun 1, 2018
2 parents fa01179 + 2011b1a commit f4afd20
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Magento\InventorySalesApi\Api\IsProductSalableInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Exception\InputException;

/**
* Adapt getStockStatusBySku for multi stocks.
Expand Down Expand Up @@ -78,7 +79,11 @@ public function afterGetStockStatusBySku(
$stockId = (int)$this->stockResolver->get(SalesChannelInterface::TYPE_WEBSITE, $websiteCode)->getStockId();

$status = (int)$this->isProductSalable->execute($productSku, $stockId);
$qty = $this->getProductSalableQty->execute($productSku, $stockId);
try {
$qty = $this->getProductSalableQty->execute($productSku, $stockId);
} catch (InputException $e) {
$qty = 0;
}

$stockStatus->setStockStatus($status);
$stockStatus->setQty($qty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Magento\InventorySalesApi\Api\IsProductSalableInterface;
use Magento\InventorySalesApi\Api\StockResolverInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Exception\InputException;

/**
* Adapt getStockStatus for multi stocks
Expand Down Expand Up @@ -88,7 +89,11 @@ public function afterGetStockStatus(
$sku = $this->getSkusByProductIds->execute([$productId])[$productId];

$status = (int)$this->isProductSalable->execute($sku, $stockId);
$qty = $this->getProductSalableQty->execute($sku, $stockId);
try {
$qty = $this->getProductSalableQty->execute($sku, $stockId);
} catch (InputException $e) {
$qty = 0;
}

$stockStatus->setStockStatus($status);
$stockStatus->setQty($qty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function getStatusDataProvider(): array
return [
['SKU-1', 1, 5.5],
['SKU-2', 1, 5],
['SKU-3', 0, 6],
['SKU-3', 0, 0], // Qty = 6 and Status = Out_Of_Stock thus Salable Quantity = 0
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function getStatusDataProvider(): array
return [
['SKU-1', 1, 5.5],
['SKU-2', 1, 5],
['SKU-3', 0, 6],
['SKU-3', 0, 0], // Qty = 6 and Status = Out_Of_Stock thus Salable Quantity = 0
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ public function testGetStatusIfScopeIdParameterIsPassed(
public function getStatusDataProvider(): array
{
return [
['store_for_eu_website', 0, 0],
['store_for_us_website', 1, 200],
['store_for_global_website', 1, 200],
['store_for_eu_website', 0, 0], // Qty not supported for complex products
['store_for_us_website', 1, 0], // Qty not supported for complex products
['store_for_global_website', 1, 0], // Qty not supported for complex products
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ public function testGetStatusIfScopeIdParameterIsPassed(
public function getStatusDataProvider(): array
{
return [
['store_for_eu_website', 0, 0],
['store_for_us_website', 1, 200],
['store_for_global_website', 1, 200],
['store_for_eu_website', 0, 0], // Qty not supported for complex products
['store_for_us_website', 1, 0], // Qty not supported for complex products
['store_for_global_website', 1, 0], // Qty not supported for complex products
];
}

Expand Down
39 changes: 37 additions & 2 deletions app/code/Magento/InventorySales/Model/GetProductSalableQty.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@

namespace Magento\InventorySales\Model;

use Magento\InventoryCatalogApi\Model\GetProductTypesBySkusInterface;
use Magento\InventoryConfigurationApi\Model\IsSourceItemManagementAllowedForProductTypeInterface;
use Magento\InventoryReservationsApi\Model\GetReservationsQuantityInterface;
use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
use Magento\InventorySalesApi\Model\GetStockItemDataInterface;
use Magento\Framework\Exception\InputException;

/**
* @inheritdoc
Expand All @@ -26,29 +29,61 @@ class GetProductSalableQty implements GetProductSalableQtyInterface
*/
private $getReservationsQuantity;

/**
* @var IsSourceItemManagementAllowedForProductTypeInterface
*/
private $isSourceItemManagementAllowedForProductType;

/**
* @var GetProductTypesBySkusInterface
*/
private $getProductTypesBySkus;

/**
* @param GetStockItemDataInterface $getStockItemData
* @param GetReservationsQuantityInterface $getReservationsQuantity
* @param IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType
* @param GetProductTypesBySkusInterface $getProductTypesBySkus
*/
public function __construct(
GetStockItemDataInterface $getStockItemData,
GetReservationsQuantityInterface $getReservationsQuantity
GetReservationsQuantityInterface $getReservationsQuantity,
IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType,
GetProductTypesBySkusInterface $getProductTypesBySkus
) {
$this->getStockItemData = $getStockItemData;
$this->getReservationsQuantity = $getReservationsQuantity;
$this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
$this->getProductTypesBySkus = $getProductTypesBySkus;
}

/**
* @inheritdoc
*/
public function execute(string $sku, int $stockId): float
{
$this->validateProductType($sku);
$stockItemData = $this->getStockItemData->execute($sku, $stockId);
if (null === $stockItemData) {

if (null === $stockItemData || (bool)$stockItemData[GetStockItemDataInterface::IS_SALABLE] === false) {
return 0;
}
$productQtyInStock = $stockItemData[GetStockItemDataInterface::QUANTITY] +
$this->getReservationsQuantity->execute($sku, $stockId);
return $productQtyInStock;
}

/**
* @param string $sku
* @throws InputException
*/
private function validateProductType(string $sku): void
{
$productType = $this->getProductTypesBySkus->execute([$sku])[$sku];
if (false === $this->isSourceItemManagementAllowedForProductType->execute($productType)) {
throw new InputException(
__('Can\'t check requested quantity for products without Source Items support.')
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ private function validateProductType(string $sku): void
$productType = $this->getProductTypesBySkus->execute([$sku])[$sku];
if (false === $this->isSourceItemManagementAllowedForProductType->execute($productType)) {
throw new LocalizedException(
__('Can\'t check requested quantity for products without Source Items supporting.')
__('Can\'t check requested quantity for products without Source Items support.')
);
}
}
Expand Down

0 comments on commit f4afd20

Please sign in to comment.