Skip to content

Commit d6aa3c1

Browse files
GetIsManageStockForProduct replaced with GetStockItemConfigurationInterface (#626)
* GetIsManageStockForProduct replaced with GetStockItemConfigurationInterface * PHP doc fixed * Dependency added and passed managed inventory true for catch failure as well * indendation fixed * tests fixed
1 parent b9b2b30 commit d6aa3c1

File tree

3 files changed

+54
-17
lines changed

3 files changed

+54
-17
lines changed

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

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
namespace Meta\Catalog\Model\Product\Feed\Builder;
2222

2323
use Magento\Catalog\Model\Product;
24-
use Magento\InventorySalesAdminUi\Model\GetIsManageStockForProduct;
24+
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
25+
use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
26+
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
2527
use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
2628
use Magento\InventorySalesApi\Api\IsProductSalableInterface;
2729
use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
@@ -65,29 +67,45 @@ class MultiSourceInventory extends InventoryRequirements implements InventoryInt
6567
private $stockQty;
6668

6769
/**
68-
* @var GetIsManageStockForProduct
70+
* @var GetStockItemConfigurationInterface
6971
*/
70-
private GetIsManageStockForProduct $getIsManageStockForProduct;
72+
private GetStockItemConfigurationInterface $getStockItemConfiguration;
73+
74+
/**
75+
* @var StockItemRepositoryInterface
76+
*/
77+
private $stockItemRepository;
78+
79+
/**
80+
* @var StockItemCriteriaInterfaceFactory
81+
*/
82+
private $stockItemCriteriaInterfaceFactory;
7183

7284
/**
7385
* @param IsProductSalableInterface $isProductSalableInterface
7486
* @param GetProductSalableQtyInterface $getProductSalableQtyInterface
7587
* @param SystemConfig $systemConfig
7688
* @param StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
77-
* @param GetIsManageStockForProduct $getIsManageStockForProduct
89+
* @param GetStockItemConfigurationInterface $getStockItemConfiguration
90+
* @param StockItemRepositoryInterface $stockItemRepository
91+
* @param StockItemCriteriaInterfaceFactory $stockItemCriteriaInterfaceFactory
7892
*/
7993
public function __construct(
80-
IsProductSalableInterface $isProductSalableInterface,
81-
GetProductSalableQtyInterface $getProductSalableQtyInterface,
82-
SystemConfig $systemConfig,
83-
StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver,
84-
GetIsManageStockForProduct $getIsManageStockForProduct
94+
IsProductSalableInterface $isProductSalableInterface,
95+
GetProductSalableQtyInterface $getProductSalableQtyInterface,
96+
SystemConfig $systemConfig,
97+
StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver,
98+
GetStockItemConfigurationInterface $getStockItemConfiguration,
99+
StockItemRepositoryInterface $stockItemRepository,
100+
StockItemCriteriaInterfaceFactory $stockItemCriteriaInterfaceFactory
85101
) {
86102
$this->isProductSalableInterface = $isProductSalableInterface;
87103
$this->getProductSalableQtyInterface = $getProductSalableQtyInterface;
88104
$this->systemConfig = $systemConfig;
89105
$this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
90-
$this->getIsManageStockForProduct = $getIsManageStockForProduct;
106+
$this->getStockItemConfiguration = $getStockItemConfiguration;
107+
$this->stockItemRepository = $stockItemRepository;
108+
$this->stockItemCriteriaInterfaceFactory = $stockItemCriteriaInterfaceFactory;
91109
}
92110

93111
/**
@@ -136,10 +154,23 @@ private function getStockQty(Product $product, int $stockId)
136154
public function isStockManagedForProduct(): bool
137155
{
138156
try {
139-
$websiteCode = $this->product->getStore()->getWebsite()->getCode();
140-
return $this->getIsManageStockForProduct->execute($this->product->getSku(), $websiteCode);
157+
$websiteId = (int)$this->product->getStore()->getWebsiteId();
158+
$stockId = $this->stockByWebsiteIdResolver->execute($websiteId)->getStockId();
159+
$stockItemConfiguration = $this->getStockItemConfiguration->execute($this->product->getSku(), $stockId);
160+
return $stockItemConfiguration->isManageStock();
141161
} catch (\Throwable $e) {
142-
return false;
162+
try {
163+
// fallback to single inventory mechanism in case of error
164+
$criteria = $this->stockItemCriteriaInterfaceFactory->create();
165+
$criteria->setProductsFilter($this->product->getId());
166+
$stocksItems = $this->stockItemRepository->getList($criteria)->getItems();
167+
$productStock = array_shift($stocksItems);
168+
return (bool)$productStock->getManageStock();
169+
} catch (\Throwable $e) {
170+
// if single inventory mechanism also fails, always return true and
171+
// let inventory count decide quantity to sell
172+
return true;
173+
}
143174
}
144175
}
145176

app/code/Meta/Catalog/Test/Unit/Model/Product/Feed/Builder/MultiSourceInventoryTest.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626

2727

2828
use Magento\Catalog\Model\Product;
29+
use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory;
30+
use Magento\CatalogInventory\Api\StockItemRepositoryInterface;
2931
use Magento\InventoryApi\Api\Data\StockInterface;
30-
use Magento\InventorySalesAdminUi\Model\GetIsManageStockForProduct;
32+
use Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface;
3133
use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
3234
use Magento\InventorySalesApi\Api\IsProductSalableInterface;
3335
use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
@@ -76,15 +78,19 @@ public function setUp(): void
7678
$this->stockByWebsiteIdResolver = $this->createStub(StockByWebsiteIdResolverInterface::class);
7779
$this->getProductSalableQtyInterface = $this->createStub(GetProductSalableQtyInterface::class);
7880

79-
$getIsManageStockForProduct = $this->createStub(GetIsManageStockForProduct::class);
81+
$getStockItemConfiguration = $this->createStub(GetStockItemConfigurationInterface::class);
82+
$stockItemRepository = $this->createStub(StockItemRepositoryInterface::class);
83+
$stockItemCriteriaInterfaceFactory = $this->createStub(StockItemCriteriaInterfaceFactory::class);
8084

8185
$this->inventory = $this->getMockBuilder(MultiSourceInventory::class)
8286
->enableOriginalConstructor()
8387
->setConstructorArgs([$this->isProductSalableInterface,
8488
$this->getProductSalableQtyInterface,
8589
$this->systemConfig,
8690
$this->stockByWebsiteIdResolver,
87-
$getIsManageStockForProduct])
91+
$getStockItemConfiguration,
92+
$stockItemRepository,
93+
$stockItemCriteriaInterfaceFactory])
8894
->onlyMethods(['isInStock', 'getStockQty', 'isStockManagedForProduct'])
8995
->getMock();
9096
}

app/code/Meta/Catalog/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"require": {
66
"php": "~7.4||~8.1.0||~8.2.0",
77
"magento/framework": "*",
8-
"magento/module-inventory-sales-admin-ui": "*",
8+
"magento/module-inventory-configuration-api": "*",
99
"magento/module-inventory-sales-api": "*",
1010
"magento/module-backend": "*",
1111
"magento/module-catalog": "*",

0 commit comments

Comments
 (0)