Skip to content

Commit 1376d4b

Browse files
authored
Merge pull request #19 from jithutj/master
multi-source stock issue of showing out of stock fix
2 parents 700c334 + 0a74be0 commit 1376d4b

File tree

1 file changed

+86
-7
lines changed
  • src/Model/Resolver/Products/DataPostProcessor

1 file changed

+86
-7
lines changed

src/Model/Resolver/Products/DataPostProcessor/Stocks.php

+86-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919
use Magento\Store\Model\ScopeInterface;
2020
use ScandiPWA\Performance\Api\ProductsDataPostProcessorInterface;
2121
use ScandiPWA\Performance\Model\Resolver\ResolveInfoFieldsTrait;
22+
use Magento\InventoryApi\Api\GetStockSourceLinksInterface;
23+
use Magento\InventoryApi\Api\Data\StockSourceLinkInterface;
24+
use Magento\InventoryCatalog\Model\GetStockIdForCurrentWebsite;
25+
use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
26+
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
2227

2328
/**
2429
* Class Images
@@ -51,6 +56,26 @@ class Stocks implements ProductsDataPostProcessorInterface
5156
*/
5257
protected $scopeConfig;
5358

59+
/**
60+
* @var GetStockSourceLinksInterface
61+
*/
62+
private $getStockSourceLinks;
63+
64+
/**
65+
* @var GetStockIdForCurrentWebsite
66+
*/
67+
private $getStockIdForCurrentWebsite;
68+
69+
/**
70+
* @var GetProductSalableQtyInterface
71+
*/
72+
private $getProductSalableQty;
73+
74+
/**
75+
* @var GetStockItemConfigurationInterface
76+
*/
77+
private $getStockItemConfiguration;
78+
5479
/**
5580
* Stocks constructor.
5681
* @param SourceItemRepositoryInterface $stockRepository
@@ -60,11 +85,19 @@ class Stocks implements ProductsDataPostProcessorInterface
6085
public function __construct(
6186
SourceItemRepositoryInterface $stockRepository,
6287
SearchCriteriaBuilder $searchCriteriaBuilder,
63-
ScopeConfigInterface $scopeConfig
88+
ScopeConfigInterface $scopeConfig,
89+
GetStockSourceLinksInterface $getStockSourceLinks,
90+
GetStockIdForCurrentWebsite $getStockIdForCurrentWebsite,
91+
GetStockItemConfigurationInterface $getStockItemConfiguration,
92+
GetProductSalableQtyInterface $getProductSalableQty
6493
) {
6594
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
6695
$this->stockRepository = $stockRepository;
6796
$this->scopeConfig = $scopeConfig;
97+
$this->getStockSourceLinks = $getStockSourceLinks;
98+
$this->getStockIdForCurrentWebsite = $getStockIdForCurrentWebsite;
99+
$this->getStockItemConfiguration = $getStockItemConfiguration;
100+
$this->getProductSalableQty = $getProductSalableQty;
68101
}
69102

70103
/**
@@ -115,6 +148,13 @@ public function process(
115148
};
116149
}
117150

151+
$stockId = $this->getStockIdForCurrentWebsite->execute();
152+
153+
if(!$stockId) {
154+
return function (&$productData) {
155+
};
156+
}
157+
118158
$productSKUs = array_map(function ($product) {
119159
return $product->getSku();
120160
}, $products);
@@ -128,8 +168,24 @@ public function process(
128168
);
129169
}
130170

171+
$criteria = $this->searchCriteriaBuilder
172+
->addFilter(StockSourceLinkInterface::STOCK_ID, $stockId)
173+
->create();
174+
175+
$sourceLinks = $this->getStockSourceLinks->execute($criteria)->getItems();
176+
177+
if (!count($sourceLinks)) {
178+
return function (&$productData) {
179+
};
180+
}
181+
182+
$sourceCodes = array_map(function ($sourceLink) {
183+
return $sourceLink->getSourceCode();
184+
}, $sourceLinks);
185+
131186
$criteria = $this->searchCriteriaBuilder
132187
->addFilter(SourceItemInterface::SKU, $productSKUs, 'in')
188+
->addFilter(SourceItemInterface::SOURCE_CODE, $sourceCodes, 'in')
133189
->create();
134190

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

144200
foreach ($stockItems as $stockItem) {
145-
$inStock = $stockItem->getStatus() === SourceItemInterface::STATUS_IN_STOCK;
146-
147201
$leftInStock = null;
148202
$qty = $stockItem->getQuantity();
203+
$sku = $stockItem->getSku();
204+
205+
$inStock = (($stockItem->getStatus() === SourceItemInterface::STATUS_IN_STOCK)
206+
and $qty > 0)? true : false;
207+
208+
if ($inStock) {
209+
$productSalableQty = $this->getProductSalableQty->execute($sku, $stockId);
210+
211+
if ($productSalableQty > 0) {
212+
$stockItemConfiguration = $this->getStockItemConfiguration->execute($sku, $stockId);
213+
$minQty = $stockItemConfiguration->getMinQty();
214+
if ($productSalableQty >= $minQty){
215+
$stockLeft = $productSalableQty - $minQty;
216+
$thresholdQty = $stockItemConfiguration->getStockThresholdQty();
217+
if ($thresholdQty !== 0) {
218+
$leftInStock = $stockLeft <= $thresholdQty ? (float)$stockLeft : null;
219+
}
220+
} else {
221+
$inStock = false;
222+
}
223+
} else {
224+
$inStock = false;
225+
}
149226

150-
if ($thresholdQty !== (float) 0) {
151-
$isThresholdPassed = $qty <= $thresholdQty;
152-
$leftInStock = $isThresholdPassed ? $qty : null;
153227
}
154228

155-
$formattedStocks[$stockItem->getSku()] = [
229+
if(isset($formattedStocks[$sku])
230+
&& $formattedStocks[$sku][self::STOCK_STATUS] == self::IN_STOCK) {
231+
continue;
232+
}
233+
234+
$formattedStocks[$sku] = [
156235
self::STOCK_STATUS => $inStock ? self::IN_STOCK : self::OUT_OF_STOCK,
157236
self::ONLY_X_LEFT_IN_STOCK => $leftInStock
158237
];

0 commit comments

Comments
 (0)