Skip to content

Commit

Permalink
Merge pull request #19 from jithutj/master
Browse files Browse the repository at this point in the history
multi-source stock issue of showing out of stock fix
  • Loading branch information
carinadues authored Jun 28, 2021
2 parents 700c334 + 0a74be0 commit 1376d4b
Showing 1 changed file with 86 additions and 7 deletions.
93 changes: 86 additions & 7 deletions src/Model/Resolver/Products/DataPostProcessor/Stocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
use Magento\Store\Model\ScopeInterface;
use ScandiPWA\Performance\Api\ProductsDataPostProcessorInterface;
use ScandiPWA\Performance\Model\Resolver\ResolveInfoFieldsTrait;
use Magento\InventoryApi\Api\GetStockSourceLinksInterface;
use Magento\InventoryApi\Api\Data\StockSourceLinkInterface;
use Magento\InventoryCatalog\Model\GetStockIdForCurrentWebsite;
use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;

/**
* Class Images
Expand Down Expand Up @@ -51,6 +56,26 @@ class Stocks implements ProductsDataPostProcessorInterface
*/
protected $scopeConfig;

/**
* @var GetStockSourceLinksInterface
*/
private $getStockSourceLinks;

/**
* @var GetStockIdForCurrentWebsite
*/
private $getStockIdForCurrentWebsite;

/**
* @var GetProductSalableQtyInterface
*/
private $getProductSalableQty;

/**
* @var GetStockItemConfigurationInterface
*/
private $getStockItemConfiguration;

/**
* Stocks constructor.
* @param SourceItemRepositoryInterface $stockRepository
Expand All @@ -60,11 +85,19 @@ class Stocks implements ProductsDataPostProcessorInterface
public function __construct(
SourceItemRepositoryInterface $stockRepository,
SearchCriteriaBuilder $searchCriteriaBuilder,
ScopeConfigInterface $scopeConfig
ScopeConfigInterface $scopeConfig,
GetStockSourceLinksInterface $getStockSourceLinks,
GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite,
GetStockItemConfigurationInterface $getStockItemConfiguration,
GetProductSalableQtyInterface $getProductSalableQty
) {
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->stockRepository = $stockRepository;
$this->scopeConfig = $scopeConfig;
$this->getStockSourceLinks = $getStockSourceLinks;
$this->getStockIdForCurrentWebsite = $getStockIdForCurrentWebsite;
$this->getStockItemConfiguration = $getStockItemConfiguration;
$this->getProductSalableQty = $getProductSalableQty;
}

/**
Expand Down Expand Up @@ -115,6 +148,13 @@ public function process(
};
}

$stockId = $this->getStockIdForCurrentWebsite->execute();

if(!$stockId) {
return function (&$productData) {
};
}

$productSKUs = array_map(function ($product) {
return $product->getSku();
}, $products);
Expand All @@ -128,8 +168,24 @@ public function process(
);
}

$criteria = $this->searchCriteriaBuilder
->addFilter(StockSourceLinkInterface::STOCK_ID, $stockId)
->create();

$sourceLinks = $this->getStockSourceLinks->execute($criteria)->getItems();

if (!count($sourceLinks)) {
return function (&$productData) {
};
}

$sourceCodes = array_map(function ($sourceLink) {
return $sourceLink->getSourceCode();
}, $sourceLinks);

$criteria = $this->searchCriteriaBuilder
->addFilter(SourceItemInterface::SKU, $productSKUs, 'in')
->addFilter(SourceItemInterface::SOURCE_CODE, $sourceCodes, 'in')
->create();

$stockItems = $this->stockRepository->getList($criteria)->getItems();
Expand All @@ -142,17 +198,40 @@ public function process(
$formattedStocks = [];

foreach ($stockItems as $stockItem) {
$inStock = $stockItem->getStatus() === SourceItemInterface::STATUS_IN_STOCK;

$leftInStock = null;
$qty = $stockItem->getQuantity();
$sku = $stockItem->getSku();

$inStock = (($stockItem->getStatus() === SourceItemInterface::STATUS_IN_STOCK)
and $qty > 0)? true : false;

if ($inStock) {
$productSalableQty = $this->getProductSalableQty->execute($sku, $stockId);

if ($productSalableQty > 0) {
$stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
$minQty = $stockItemConfiguration->getMinQty();
if ($productSalableQty >= $minQty){
$stockLeft = $productSalableQty - $minQty;
$thresholdQty = $stockItemConfiguration->getStockThresholdQty();
if ($thresholdQty !== 0) {
$leftInStock = $stockLeft <= $thresholdQty ? (float)$stockLeft : null;
}
} else {
$inStock = false;
}
} else {
$inStock = false;
}

if ($thresholdQty !== (float) 0) {
$isThresholdPassed = $qty <= $thresholdQty;
$leftInStock = $isThresholdPassed ? $qty : null;
}

$formattedStocks[$stockItem->getSku()] = [
if(isset($formattedStocks[$sku])
&& $formattedStocks[$sku][self::STOCK_STATUS] == self::IN_STOCK) {
continue;
}

$formattedStocks[$sku] = [
self::STOCK_STATUS => $inStock ? self::IN_STOCK : self::OUT_OF_STOCK,
self::ONLY_X_LEFT_IN_STOCK => $leftInStock
];
Expand Down

0 comments on commit 1376d4b

Please sign in to comment.