|
21 | 21 | namespace Meta\Catalog\Model\Product\Feed\Builder;
|
22 | 22 |
|
23 | 23 | 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; |
25 | 27 | use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
|
26 | 28 | use Magento\InventorySalesApi\Api\IsProductSalableInterface;
|
27 | 29 | use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
|
@@ -65,29 +67,45 @@ class MultiSourceInventory extends InventoryRequirements implements InventoryInt
|
65 | 67 | private $stockQty;
|
66 | 68 |
|
67 | 69 | /**
|
68 |
| - * @var GetIsManageStockForProduct |
| 70 | + * @var GetStockItemConfigurationInterface |
69 | 71 | */
|
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; |
71 | 83 |
|
72 | 84 | /**
|
73 | 85 | * @param IsProductSalableInterface $isProductSalableInterface
|
74 | 86 | * @param GetProductSalableQtyInterface $getProductSalableQtyInterface
|
75 | 87 | * @param SystemConfig $systemConfig
|
76 | 88 | * @param StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver
|
77 |
| - * @param GetIsManageStockForProduct $getIsManageStockForProduct |
| 89 | + * @param GetStockItemConfigurationInterface $getStockItemConfiguration |
| 90 | + * @param StockItemRepositoryInterface $stockItemRepository |
| 91 | + * @param StockItemCriteriaInterfaceFactory $stockItemCriteriaInterfaceFactory |
78 | 92 | */
|
79 | 93 | 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 |
85 | 101 | ) {
|
86 | 102 | $this->isProductSalableInterface = $isProductSalableInterface;
|
87 | 103 | $this->getProductSalableQtyInterface = $getProductSalableQtyInterface;
|
88 | 104 | $this->systemConfig = $systemConfig;
|
89 | 105 | $this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver;
|
90 |
| - $this->getIsManageStockForProduct = $getIsManageStockForProduct; |
| 106 | + $this->getStockItemConfiguration = $getStockItemConfiguration; |
| 107 | + $this->stockItemRepository = $stockItemRepository; |
| 108 | + $this->stockItemCriteriaInterfaceFactory = $stockItemCriteriaInterfaceFactory; |
91 | 109 | }
|
92 | 110 |
|
93 | 111 | /**
|
@@ -136,10 +154,23 @@ private function getStockQty(Product $product, int $stockId)
|
136 | 154 | public function isStockManagedForProduct(): bool
|
137 | 155 | {
|
138 | 156 | 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(); |
141 | 161 | } 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 | + } |
143 | 174 | }
|
144 | 175 | }
|
145 | 176 |
|
|
0 commit comments