From 3d69ac9e57003bb6229143e7bf84adf12aa582d5 Mon Sep 17 00:00:00 2001 From: Bashev Date: Sat, 14 Jun 2025 09:30:50 +0300 Subject: [PATCH 1/4] [WIP] Filter only InStock child products in the products index. --- .../Indexer/Fulltext/Datasource/AttributeData.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/AttributeData.php b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/AttributeData.php index 219224da1..0722b7705 100644 --- a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/AttributeData.php +++ b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/AttributeData.php @@ -15,9 +15,12 @@ namespace Smile\ElasticsuiteCatalog\Model\ResourceModel\Product\Indexer\Fulltext\Datasource; use Magento\Catalog\Api\Data\ProductInterface; +use Magento\CatalogInventory\Model\Configuration; use Magento\Eav\Model\Config; use Magento\Catalog\Model\Product; +use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\EntityManager\MetadataPool; +use Magento\Store\Model\ScopeInterface; use Smile\ElasticsuiteCatalog\Model\ResourceModel\Eav\Indexer\Fulltext\Datasource\AbstractAttributeData; use Magento\Framework\App\ResourceConnection; use Magento\Store\Model\StoreManagerInterface; @@ -55,6 +58,7 @@ class AttributeData extends AbstractAttributeData * @var Config */ private $eavConfig; + private ScopeConfigInterface $scopeConfig; /** * Constructor. @@ -72,11 +76,13 @@ public function __construct( MetadataPool $metadataPool, ProductType $catalogProductType, Config $eavConfig, + ScopeConfigInterface $scopeConfig, $entityType = ProductInterface::class ) { parent::__construct($resource, $storeManager, $metadataPool, $entityType); $this->catalogProductType = $catalogProductType; $this->eavConfig = $eavConfig; + $this->scopeConfig = $scopeConfig; } /** @@ -229,6 +235,15 @@ private function getRelationQuery($relation, $parentIds, $storeId) ) ->where("parent.{$entityIdField} in (?)", $parentIds); + if (!$this->scopeConfig->getValue(Configuration::XML_PATH_SHOW_OUT_OF_STOCK, ScopeInterface::SCOPE_STORE)) { + // Added Only InStock Products. + $select->joinInner( + ['stock' => $this->getTable('cataloginventory_stock_item')], + new \Zend_Db_Expr("child.{$entityIdField} = stock.product_id AND stock.is_in_stock = 1"), + [] + ); + } + if ($relation->getWhere() !== null) { $select->where($relation->getWhere()); } From 6ece7db2832c6b58c5457bb631429c1fc300918d Mon Sep 17 00:00:00 2001 From: Bashev Date: Sat, 14 Jun 2025 09:50:45 +0300 Subject: [PATCH 2/4] code sniffer coverage --- .../Indexer/Fulltext/Datasource/AttributeData.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/AttributeData.php b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/AttributeData.php index 0722b7705..f5f88f702 100644 --- a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/AttributeData.php +++ b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/AttributeData.php @@ -58,16 +58,23 @@ class AttributeData extends AbstractAttributeData * @var Config */ private $eavConfig; + + /** + * Scope Config + * + * @var ScopeConfigInterface + */ private ScopeConfigInterface $scopeConfig; /** * Constructor. * - * @param ResourceConnection $resource Database adpater. + * @param ResourceConnection $resource Database adapter. * @param StoreManagerInterface $storeManager Store manager. * @param MetadataPool $metadataPool Metadata Pool. * @param ProductType $catalogProductType Product type. * @param Config $eavConfig Eav config. + * @param ScopeConfigInterface $scopeConfig Scope Config. * @param string $entityType Product entity type. */ public function __construct( From 2603945e777c86eafd337d817e073c4f34de5ce4 Mon Sep 17 00:00:00 2001 From: Bashev Date: Sat, 14 Jun 2025 10:10:23 +0300 Subject: [PATCH 3/4] remove unnecessary scope. --- .../Indexer/Fulltext/Datasource/AttributeData.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/AttributeData.php b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/AttributeData.php index f5f88f702..ac55af909 100644 --- a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/AttributeData.php +++ b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/AttributeData.php @@ -225,6 +225,7 @@ private function getRelationQuery($relation, $parentIds, $storeId) $entityIdField = $this->getEntityMetaData($this->getEntityTypeId())->getIdentifierField(); $entityTable = $this->getTable($this->getEntityMetaData($this->getEntityTypeId())->getEntityTable()); $relationTable = $this->getTable($relation->getTable()); + $inventoryTable = $this->getTable('cataloginventory_stock_item'); $parentFieldName = $relation->getParentFieldName(); $childFieldName = $relation->getChildFieldName(); @@ -242,10 +243,13 @@ private function getRelationQuery($relation, $parentIds, $storeId) ) ->where("parent.{$entityIdField} in (?)", $parentIds); - if (!$this->scopeConfig->getValue(Configuration::XML_PATH_SHOW_OUT_OF_STOCK, ScopeInterface::SCOPE_STORE)) { - // Added Only InStock Products. + /** + * If Catalog - Inventory - Stock Options - Display of Stock Products is set to NO, + * then exclude this children from the query results. + */ + if (!$this->scopeConfig->getValue(Configuration::XML_PATH_SHOW_OUT_OF_STOCK)) { $select->joinInner( - ['stock' => $this->getTable('cataloginventory_stock_item')], + ['stock' => $inventoryTable], new \Zend_Db_Expr("child.{$entityIdField} = stock.product_id AND stock.is_in_stock = 1"), [] ); From d95b4e04c05659faccf36e48158d4f043483021b Mon Sep 17 00:00:00 2001 From: Bashev Date: Sat, 14 Jun 2025 16:40:55 +0300 Subject: [PATCH 4/4] fix phpmd CouplingBetweenObjects --- .../Product/Indexer/Fulltext/Datasource/AttributeData.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/AttributeData.php b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/AttributeData.php index ac55af909..bcd5b4c60 100644 --- a/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/AttributeData.php +++ b/src/module-elasticsuite-catalog/Model/ResourceModel/Product/Indexer/Fulltext/Datasource/AttributeData.php @@ -32,6 +32,8 @@ * @category Smile * @package Smile\ElasticsuiteCatalog * @author Aurelien FOUCRET + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class AttributeData extends AbstractAttributeData {