storeManager = $storeManager; $this->resourceConnection = $resourceConnection; $this->eavConfig = $eavConfig; $this->scopeConfig = $scopeConfig; $this->aliasResolver = $aliasResolver; $this->frontendResource = $frontendResource ?: ObjectManager::getInstance() ->get(\Magento\Catalog\Model\ResourceModel\Product\Indexer\Eav\FrontendResource::class); $this->indexerStockFrontendResource = $indexerStockFrontendResource ?: ObjectManager::getInstance() ->get(\Magento\CatalogInventory\Model\ResourceModel\Indexer\Stock\FrontendResource::class); } /** * {@inheritDoc} * @throws \Magento\Framework\Exception\LocalizedException */ public function apply( \Magento\Framework\Search\Request\FilterInterface $filter, \Magento\Framework\DB\Select $select ) { $alias = $this->aliasResolver->getAlias($filter); $attribute = $this->getAttributeByCode($filter->getField()); $joinCondition = sprintf( 'search_index.entity_id = %1$s.entity_id AND %1$s.attribute_id = %2$d AND %1$s.store_id = %3$d', $alias, $attribute->getId(), $this->storeManager->getStore()->getId() ); $select->joinLeft( [$alias => $this->frontendResource->getMainTable()], $joinCondition, [] ); if ($this->isAddStockFilter()) { $stockAlias = $alias . AliasResolver::STOCK_FILTER_SUFFIX; $select->joinLeft( [ $stockAlias => $this->indexerStockFrontendResource->getMainTable(), ], sprintf('%2$s.product_id = %1$s.source_id', $alias, $stockAlias), [] ); } return true; } /** * @param string $field * @return \Magento\Catalog\Model\ResourceModel\Eav\Attribute * @throws \Magento\Framework\Exception\LocalizedException */ private function getAttributeByCode($field) { return $this->eavConfig->getAttribute(\Magento\Catalog\Model\Product::ENTITY, $field); } /** * @return bool */ private function isAddStockFilter() { $isShowOutOfStock = $this->scopeConfig->isSetFlag( 'cataloginventory/options/show_out_of_stock', ScopeInterface::SCOPE_STORE ); return false === $isShowOutOfStock; } }