|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +namespace Meta\Catalog\Model\Product\Feed\Builder; |
| 22 | + |
| 23 | +use Meta\BusinessExtension\Model\System\Config as SystemConfig; |
| 24 | +use Magento\Catalog\Model\Product; |
| 25 | +use Magento\CatalogInventory\Api\Data\StockItemInterface; |
| 26 | +use Magento\CatalogInventory\Api\StockItemCriteriaInterfaceFactory; |
| 27 | +use Magento\CatalogInventory\Api\StockItemRepositoryInterface; |
| 28 | + |
| 29 | +class Inventory implements InventoryInterface |
| 30 | +{ |
| 31 | + /** |
| 32 | + * @var StockItemRepositoryInterface |
| 33 | + */ |
| 34 | + private $stockItemRepository; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var StockItemCriteriaInterfaceFactory |
| 38 | + */ |
| 39 | + private $stockItemCriteriaInterfaceFactory; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var Product |
| 43 | + */ |
| 44 | + private $product; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var SystemConfig |
| 48 | + */ |
| 49 | + private $systemConfig; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var StockItemInterface |
| 53 | + */ |
| 54 | + private $productStock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @param StockItemRepositoryInterface $stockItemRepository |
| 58 | + * @param StockItemCriteriaInterfaceFactory $stockItemCriteriaInterfaceFactory |
| 59 | + * @param SystemConfig $systemConfig |
| 60 | + */ |
| 61 | + public function __construct( |
| 62 | + StockItemRepositoryInterface $stockItemRepository, |
| 63 | + StockItemCriteriaInterfaceFactory $stockItemCriteriaInterfaceFactory, |
| 64 | + SystemConfig $systemConfig |
| 65 | + ) { |
| 66 | + $this->stockItemRepository = $stockItemRepository; |
| 67 | + $this->stockItemCriteriaInterfaceFactory = $stockItemCriteriaInterfaceFactory; |
| 68 | + $this->systemConfig = $systemConfig; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Get stock item |
| 73 | + * |
| 74 | + * @param Product $product |
| 75 | + * @return StockItemInterface|null |
| 76 | + */ |
| 77 | + public function getStockItem(Product $product): ?StockItemInterface |
| 78 | + { |
| 79 | + $criteria = $this->stockItemCriteriaInterfaceFactory->create(); |
| 80 | + $criteria->setProductsFilter($product->getId()); |
| 81 | + $stocksItems = $this->stockItemRepository->getList($criteria)->getItems(); |
| 82 | + return array_shift($stocksItems); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Init inventory for product |
| 87 | + * |
| 88 | + * @param Product $product |
| 89 | + * @return $this |
| 90 | + */ |
| 91 | + public function initInventoryForProduct(Product $product): Inventory |
| 92 | + { |
| 93 | + $this->product = $product; |
| 94 | + $this->productStock = $this->getStockItem($product); |
| 95 | + return $this; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Get product stock status |
| 100 | + * |
| 101 | + * @return string |
| 102 | + */ |
| 103 | + public function getAvailability(): string |
| 104 | + { |
| 105 | + return $this->productStock && $this->productStock->getIsInStock() |
| 106 | + && ($this->getInventory() - $this->systemConfig->getOutOfStockThreshold() > 0) |
| 107 | + ? self::STATUS_IN_STOCK : self::STATUS_OUT_OF_STOCK; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Get available product qty |
| 112 | + * |
| 113 | + * @return int |
| 114 | + */ |
| 115 | + public function getInventory(): int |
| 116 | + { |
| 117 | + if (!($this->product && $this->productStock)) { |
| 118 | + return 0; |
| 119 | + } |
| 120 | + |
| 121 | + if (!$this->productStock->getManageStock()) { |
| 122 | + return self::UNMANAGED_STOCK_QTY; // fake quantity to make product available if Manage Stock is off |
| 123 | + } |
| 124 | + |
| 125 | + $outOfStockThreshold = $this->systemConfig->getOutOfStockThreshold($this->product->getStoreId()); |
| 126 | + return (int)max($this->productStock->getQty() - $outOfStockThreshold, 0); |
| 127 | + } |
| 128 | +} |
0 commit comments