From 77a416e4f74f1ab3f58403aa31e4151e735afb32 Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Thu, 17 Apr 2025 11:45:29 +0200 Subject: [PATCH 1/5] MSI compatibility while being backward compatible --- Service/Product/RecordBuilder.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/Service/Product/RecordBuilder.php b/Service/Product/RecordBuilder.php index 78d046ad4..cadda2dc7 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; @@ -810,8 +807,6 @@ protected function getSanitizedArrayValues(array $values, string $attributeName) */ public function productIsInStock($product, $storeId): bool { - $stockItem = $this->stockRegistry->getStockItem($product->getId()); - - return $product->isSaleable() && $stockItem->getIsInStock(); + return $product->getIsSalable(); } } From c3014d8e292e2e8a72490f977d62a9936ee4ac85 Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Thu, 17 Apr 2025 14:10:52 +0200 Subject: [PATCH 2/5] Use compatible MSI stock data collection modifier --- Helper/Entity/ProductHelper.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Helper/Entity/ProductHelper.php b/Helper/Entity/ProductHelper.php index b2197a627..fa400f466 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 + ); } /** @@ -286,6 +287,8 @@ public function getIndexSettings(?int $storeId = null): array $customRanking = $this->getCustomRanking($storeId); $unretrievableAttributes = $this->getUnretrieveableAttributes($storeId); $attributesForFaceting = $this->getAttributesForFaceting($storeId); + $attributesForFaceting[] = 'searchable(categories.level0)'; + $attributesForFaceting[] = 'searchable(categories)'; $indexSettings = [ 'searchableAttributes' => $searchableAttributes, From ebb2bcf699b7c9b3f18e8e96026c6250cdd1b226 Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Thu, 17 Apr 2025 14:14:01 +0200 Subject: [PATCH 3/5] Remove changes from custom patch --- Helper/Entity/ProductHelper.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/Helper/Entity/ProductHelper.php b/Helper/Entity/ProductHelper.php index fa400f466..a83e16645 100755 --- a/Helper/Entity/ProductHelper.php +++ b/Helper/Entity/ProductHelper.php @@ -287,8 +287,6 @@ public function getIndexSettings(?int $storeId = null): array $customRanking = $this->getCustomRanking($storeId); $unretrievableAttributes = $this->getUnretrieveableAttributes($storeId); $attributesForFaceting = $this->getAttributesForFaceting($storeId); - $attributesForFaceting[] = 'searchable(categories.level0)'; - $attributesForFaceting[] = 'searchable(categories)'; $indexSettings = [ 'searchableAttributes' => $searchableAttributes, From bf30e885b0daf1cbee60c694fb8653af1b5f8b8b Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Thu, 17 Apr 2025 14:14:41 +0200 Subject: [PATCH 4/5] Force type with bool cast --- Service/Product/RecordBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Service/Product/RecordBuilder.php b/Service/Product/RecordBuilder.php index cadda2dc7..d33532c64 100644 --- a/Service/Product/RecordBuilder.php +++ b/Service/Product/RecordBuilder.php @@ -807,6 +807,6 @@ protected function getSanitizedArrayValues(array $values, string $attributeName) */ public function productIsInStock($product, $storeId): bool { - return $product->getIsSalable(); + return (bool)$product->getIsSalable(); } } From 524c44085778be2b4fa706ec1f343bcdc09bf7fe Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Wed, 23 Apr 2025 16:08:14 +0200 Subject: [PATCH 5/5] Use correct salable method --- Service/Product/RecordBuilder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Service/Product/RecordBuilder.php b/Service/Product/RecordBuilder.php index d33532c64..705fbdc61 100644 --- a/Service/Product/RecordBuilder.php +++ b/Service/Product/RecordBuilder.php @@ -801,12 +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 { - return (bool)$product->getIsSalable(); + return (bool)$product->isSalable(); } }