Skip to content

Commit 80b2b6c

Browse files
[Catalog] Unmanaged stock handled in Multi source inventory (#485)
* Unmanaged stock handled in MSI * comment updated * unmanaged inventory fixed for inventory class * Catalog inventory module replaced with invetory sales module * undeclared dependency added
1 parent 881b723 commit 80b2b6c

File tree

4 files changed

+44
-5
lines changed

4 files changed

+44
-5
lines changed

app/code/Meta/Catalog/Model/Product/Feed/Builder/Inventory.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,14 @@ public function initInventoryForProduct(Product $product): Inventory
102102
*/
103103
public function getAvailability(): string
104104
{
105+
$inventory = $this->getInventory();
106+
107+
// unmanaged stock is always available
108+
if ($inventory === self::UNMANAGED_STOCK_QTY) {
109+
return self::STATUS_IN_STOCK;
110+
}
105111
return $this->productStock && $this->productStock->getIsInStock()
106-
&& ($this->getInventory() > 0)
112+
&& ($inventory > 0)
107113
? self::STATUS_IN_STOCK : self::STATUS_OUT_OF_STOCK;
108114
}
109115

@@ -119,7 +125,7 @@ public function getInventory(): int
119125
}
120126

121127
if (!$this->productStock->getManageStock()) {
122-
return self::UNMANAGED_STOCK_QTY; // fake quantity to make product available if Manage Stock is off
128+
return self::UNMANAGED_STOCK_QTY;
123129
}
124130

125131
$outOfStockThreshold = $this->systemConfig->getOutOfStockThreshold($this->product->getStoreId());

app/code/Meta/Catalog/Model/Product/Feed/Builder/InventoryInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ interface InventoryInterface
2727

2828
public const STATUS_OUT_OF_STOCK = 'out of stock';
2929

30-
public const UNMANAGED_STOCK_QTY = 9999;
30+
// -1 is unmanaged inventory in Meta catalog
31+
public const UNMANAGED_STOCK_QTY = -1;
3132

3233
/**
3334
* Init inventory for product

app/code/Meta/Catalog/Model/Product/Feed/Builder/MultiSourceInventory.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@
2020

2121
namespace Meta\Catalog\Model\Product\Feed\Builder;
2222

23-
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
2423
use Magento\Catalog\Model\Product;
24+
use Magento\InventorySalesAdminUi\Model\GetIsManageStockForProduct;
2525
use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
2626
use Magento\InventorySalesApi\Api\IsProductSalableInterface;
2727
use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
28+
use Meta\BusinessExtension\Model\System\Config as SystemConfig;
2829

2930
class MultiSourceInventory implements InventoryInterface
3031
{
@@ -63,22 +64,30 @@ class MultiSourceInventory implements InventoryInterface
6364
*/
6465
private $stockQty;
6566

67+
/**
68+
* @var GetIsManageStockForProduct
69+
*/
70+
private GetIsManageStockForProduct $getIsManageStockForProduct;
71+
6672
/**
6773
* @param IsProductSalableInterface $isProductSalableInterface
6874
* @param GetProductSalableQtyInterface $getProductSalableQtyInterface
6975
* @param SystemConfig $systemConfig
7076
* @param StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
77+
* @param GetIsManageStockForProduct $getIsManageStockForProduct
7178
*/
7279
public function __construct(
7380
IsProductSalableInterface $isProductSalableInterface,
7481
GetProductSalableQtyInterface $getProductSalableQtyInterface,
7582
SystemConfig $systemConfig,
76-
StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
83+
StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver,
84+
GetIsManageStockForProduct $getIsManageStockForProduct
7785
) {
7886
$this->isProductSalableInterface = $isProductSalableInterface;
7987
$this->getProductSalableQtyInterface = $getProductSalableQtyInterface;
8088
$this->systemConfig = $systemConfig;
8189
$this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
90+
$this->getIsManageStockForProduct = $getIsManageStockForProduct;
8291
}
8392

8493
/**
@@ -119,6 +128,21 @@ private function getStockQty(Product $product, int $stockId)
119128
}
120129
}
121130

131+
/**
132+
* Checks if product is having managed stock
133+
*
134+
* @return bool
135+
*/
136+
public function isStockManagedForProduct(): bool
137+
{
138+
try {
139+
$websiteCode = $this->product->getStore()->getWebsite()->getCode();
140+
return $this->getIsManageStockForProduct->execute($this->product->getSku(), $websiteCode);
141+
} catch (\Throwable $e) {
142+
return false;
143+
}
144+
}
145+
122146
/**
123147
* Initiate inventory for the product
124148
*
@@ -142,6 +166,10 @@ public function initInventoryForProduct(Product $product): MultiSourceInventory
142166
*/
143167
public function getAvailability(): string
144168
{
169+
// unmanaged stock is always available
170+
if (!$this->isStockManagedForProduct()) {
171+
return self::STATUS_IN_STOCK;
172+
}
145173
return $this->getInventory() && $this->stockStatus ? self::STATUS_IN_STOCK : self::STATUS_OUT_OF_STOCK;
146174
}
147175

@@ -155,6 +183,9 @@ public function getInventory(): int
155183
if (!$this->product) {
156184
return 0;
157185
}
186+
if (!$this->isStockManagedForProduct()) {
187+
return self::UNMANAGED_STOCK_QTY;
188+
}
158189
$outOfStockThreshold = $this->systemConfig->getOutOfStockThreshold($this->product->getStoreId());
159190
$quantityAvailableForCatalog = (int) $this->stockQty - $outOfStockThreshold;
160191
return $quantityAvailableForCatalog > 0 ? $quantityAvailableForCatalog : 0;

app/code/Meta/Catalog/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"require": {
66
"php": "~8.1.0||~8.2.0",
77
"magento/framework": "*",
8+
"magento/module-inventory-sales-admin-ui": "*",
89
"magento/module-inventory-sales-api": "*",
910
"magento/module-backend": "*",
1011
"magento/module-catalog": "*",

0 commit comments

Comments
 (0)