diff --git a/Helper/Entity/ProductHelper.php b/Helper/Entity/ProductHelper.php index b2197a627..a83e16645 100755 --- a/Helper/Entity/ProductHelper.php +++ b/Helper/Entity/ProductHelper.php @@ -19,7 +19,7 @@ use Magento\Catalog\Model\Product\Visibility; use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection; use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; -use Magento\CatalogInventory\Helper\Stock; +use Magento\CatalogInventory\Model\ResourceModel\Stock\Status as StockStatus; use Magento\Customer\Api\GroupExcludedWebsiteRepositoryInterface; use Magento\Customer\Model\ResourceModel\Group\Collection as GroupCollection; use Magento\Directory\Model\Currency as CurrencyHelper; @@ -80,7 +80,6 @@ public function __construct( protected StoreManagerInterface $storeManager, protected ManagerInterface $eventManager, protected Visibility $visibility, - protected Stock $stockHelper, protected CurrencyHelper $currencyManager, protected Type $productType, protected CollectionFactory $productCollectionFactory, @@ -90,6 +89,7 @@ public function __construct( protected ReplicaManagerInterface $replicaManager, protected ProductInterfaceFactory $productFactory, protected ProductRecordBuilder $productRecordBuilder, + protected StockStatus $stockStatus, ) { parent::__construct($indexNameFetcher); @@ -239,9 +239,10 @@ public function getProductCollectionQuery( */ protected function addStockFilter($products, $storeId): void { - if ($this->configHelper->getShowOutOfStock($storeId) === false) { - $this->stockHelper->addInStockFilterToCollection($products); - } + $this->stockStatus->addStockDataToCollection( + $products, + $this->configHelper->getShowOutOfStock($storeId) === false + ); } /** diff --git a/Service/Product/RecordBuilder.php b/Service/Product/RecordBuilder.php index 78d046ad4..705fbdc61 100644 --- a/Service/Product/RecordBuilder.php +++ b/Service/Product/RecordBuilder.php @@ -268,8 +268,7 @@ protected function addImageData(array $customData, Product $product, $additional public function addInStock($defaultData, $customData, Product $product) { if (isset($defaultData['in_stock']) === false) { - $stockItem = $this->stockRegistry->getStockItem($product->getId()); - $customData['in_stock'] = $product->isSaleable() && $stockItem->getIsInStock(); + $customData['in_stock'] = $this->productIsInStock($product, $product->getStoreId()); } return $customData; @@ -288,10 +287,8 @@ protected function addStockQty($defaultData, $customData, $additionalAttributes, && $this->isAttributeEnabled($additionalAttributes, 'stock_qty')) { $customData['stock_qty'] = 0; - $stockItem = $this->stockRegistry->getStockItem($product->getId()); - if ($stockItem) { - $customData['stock_qty'] = (int)$stockItem->getQty(); - } + $stockStatus = $this->stockRegistry->getStockStatus($product->getId(), $product->getStore()->getWebsiteId()); + $customData['stock_qty'] = (int)$stockStatus->getQty(); } return $customData; @@ -804,14 +801,12 @@ protected function getSanitizedArrayValues(array $values, string $attributeName) * Returns is product in stock * * @param Product $product - * @param int $storeId + * @param int $storeId (deprecated) * * @return bool */ public function productIsInStock($product, $storeId): bool { - $stockItem = $this->stockRegistry->getStockItem($product->getId()); - - return $product->isSaleable() && $stockItem->getIsInStock(); + return (bool)$product->isSalable(); } }