Created
April 5, 2022 12:48
-
-
Save jordanvector/82614fd4a34b24041f915cecebcac89f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From b5109dc14098dfcddd69d5639b25960ff2165f97 Mon Sep 17 00:00:00 2001 | |
From: Jordan Schinella <[email protected]> | |
Date: Wed, 23 Mar 2022 18:10:48 -0400 | |
Subject: [PATCH] fix products missing from search | |
--- | |
Model/Indexer/FilterProductByStock.php | 65 ++++++------------- | |
...ildProductFilterByInventoryStockPlugin.php | 28 ++++---- | |
etc/di.xml | 2 +- | |
3 files changed, 38 insertions(+), 57 deletions(-) | |
diff --git a/Model/Indexer/FilterProductByStock.php b/Model/Indexer/FilterProductByStock.php | |
index 93f9380..c1f9026 100644 | |
--- a/Model/Indexer/FilterProductByStock.php | |
+++ b/Model/Indexer/FilterProductByStock.php | |
@@ -9,7 +9,7 @@ namespace Magento\InventoryCatalogSearch\Model\Indexer; | |
use Magento\Framework\App\ResourceConnection; | |
use Magento\Framework\DB\Select; | |
-use Magento\Framework\EntityManager\MetadataPool; | |
+use Magento\Framework\Exception\NoSuchEntityException; | |
use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface; | |
use Magento\InventoryIndexer\Model\StockIndexTableNameResolverInterface; | |
use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface; | |
@@ -45,11 +45,6 @@ class FilterProductByStock | |
*/ | |
private $storeRepository; | |
- /** | |
- * @var MetadataPool | |
- */ | |
- private $metadataPool; | |
- | |
/** | |
* @var array | |
*/ | |
@@ -61,7 +56,6 @@ class FilterProductByStock | |
* @param StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver | |
* @param StockIndexTableNameResolverInterface $stockIndexTableNameResolver | |
* @param StoreRepositoryInterface $storeRepository | |
- * @param MetadataPool $metadataPool | |
* @param array $selectModifiersPool | |
*/ | |
public function __construct( | |
@@ -70,70 +64,53 @@ class FilterProductByStock | |
StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver, | |
StockIndexTableNameResolverInterface $stockIndexTableNameResolver, | |
StoreRepositoryInterface $storeRepository, | |
- MetadataPool $metadataPool, | |
array $selectModifiersPool = [] | |
- ) { | |
+ ) | |
+ { | |
$this->defaultStockProvider = $defaultStockProvider; | |
$this->resourceConnection = $resourceConnection; | |
$this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver; | |
$this->stockIndexTableNameResolver = $stockIndexTableNameResolver; | |
$this->storeRepository = $storeRepository; | |
- $this->metadataPool = $metadataPool; | |
$this->selectModifiersPool = $selectModifiersPool; | |
} | |
/** | |
* Return filtered product by stock status for product indexer | |
* | |
- * @param array $products | |
+ * @param Select $select | |
* @param int $storeId | |
- * @return array | |
+ * @return Select | |
+ * @throws NoSuchEntityException | |
*/ | |
- public function execute(array $products, int $storeId): array | |
+ public function execute(Select $select, int $storeId): Select | |
{ | |
$store = $this->storeRepository->getById($storeId); | |
- $stock = $this->stockByWebsiteIdResolver->execute((int)$store->getWebsiteId()); | |
- $stockId = $stock->getStockId(); | |
- if ($this->defaultStockProvider->getId() === $stockId) { | |
- return $products; | |
+ try { | |
+ $stock = $this->stockByWebsiteIdResolver->execute((int)$store->getWebsiteId()); | |
+ } catch (NoSuchEntityException $exception) { | |
+ return $select; | |
} | |
+ $stockId = $stock->getStockId(); | |
$stockTable = $this->stockIndexTableNameResolver->execute($stockId); | |
- $productIds = array_column($products, 'entity_id'); | |
- $filteredProductIds = $this->getStockStatusesFromCustomStock($productIds, $stockTable); | |
- return array_filter($products, function ($product) use ($filteredProductIds) { | |
- return in_array($product['entity_id'], $filteredProductIds); | |
- }); | |
- } | |
- | |
- /** | |
- * Get product stock statuses on custom stock. | |
- * | |
- * @param array $productIds | |
- * @param string $stockTable | |
- * @return array | |
- */ | |
- private function getStockStatusesFromCustomStock(array $productIds, string $stockTable): array | |
- { | |
$connection = $this->resourceConnection->getConnection(); | |
- if (!$connection->isTableExists($stockTable)) { | |
- return []; | |
+ | |
+ if ($this->defaultStockProvider->getId() === $stockId || | |
+ !$connection->isTableExists($stockTable)) { | |
+ return $select; | |
} | |
- $select = $connection->select(); | |
- $select->from( | |
- ['product' => $this->resourceConnection->getTableName('catalog_product_entity')], | |
- ['entity_id'] | |
- ); | |
$select->joinInner( | |
['stock' => $stockTable], | |
- 'product.sku = stock.sku', | |
+ 'e.sku = stock.sku', | |
[] | |
); | |
- $select->where('product.entity_id IN (?)', $productIds); | |
+ | |
$select->where('stock.is_salable = ?', 1); | |
$this->applySelectModifiers($select, $stockTable); | |
- return $connection->fetchCol($select); | |
+ | |
+ return $select; | |
} | |
/** | |
@@ -149,4 +126,4 @@ class FilterProductByStock | |
$selectModifier->modify($select, $stockTable); | |
} | |
} | |
-} | |
+} | |
\ No newline at end of file | |
diff --git a/Plugin/CatalogSearch/Model/Indexer/ChildProductFilterByInventoryStockPlugin.php b/Plugin/CatalogSearch/Model/Indexer/ChildProductFilterByInventoryStockPlugin.php | |
index 3add9a4..d41dc15 100644 | |
--- a/Plugin/CatalogSearch/Model/Indexer/ChildProductFilterByInventoryStockPlugin.php | |
+++ b/Plugin/CatalogSearch/Model/Indexer/ChildProductFilterByInventoryStockPlugin.php | |
@@ -8,7 +8,9 @@ declare(strict_types=1); | |
namespace Magento\InventoryCatalogSearch\Plugin\CatalogSearch\Model\Indexer; | |
use Magento\CatalogInventory\Api\StockConfigurationInterface; | |
-use Magento\CatalogSearch\Model\Indexer\Fulltext\Action\DataProvider; | |
+use Magento\CatalogSearch\Model\Indexer\Fulltext\Action\GetSearchableProductsSelect; | |
+use Magento\Framework\DB\Select; | |
+use Magento\Framework\Exception\NoSuchEntityException; | |
use Magento\InventoryCatalogSearch\Model\Indexer\FilterProductByStock; | |
/** | |
@@ -41,20 +43,22 @@ class ChildProductFilterByInventoryStockPlugin | |
/** | |
* Filter out of stock options for composite products. | |
* | |
- * @param DataProvider $subject | |
- * @param array $result | |
- * @param string $storeId | |
- * @return array | |
+ * @param GetSearchableProductsSelect $subject | |
+ * @param Select $result | |
+ * @param int $storeId | |
+ * @return Select | |
+ * @throws NoSuchEntityException | |
* @SuppressWarnings(PHPMD.UnusedFormalParameter) | |
*/ | |
- public function afterGetSearchableProducts( | |
- DataProvider $subject, | |
- array $result, | |
- string $storeId | |
- ) { | |
+ public function afterExecute( | |
+ GetSearchableProductsSelect $subject, | |
+ Select $result, | |
+ int $storeId | |
+ ): Select | |
+ { | |
if ($this->stockConfiguration->isShowOutOfStock($storeId) || empty($result)) { | |
return $result; | |
} | |
- return $this->filterProductByStock->execute($result, (int)$storeId); | |
+ return $this->filterProductByStock->execute($result, $storeId); | |
} | |
-} | |
+} | |
\ No newline at end of file | |
diff --git a/etc/di.xml b/etc/di.xml | |
index 561f421..1554816 100644 | |
--- a/etc/di.xml | |
+++ b/etc/di.xml | |
@@ -6,7 +6,7 @@ | |
*/ | |
--> | |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> | |
- <type name="Magento\CatalogSearch\Model\Indexer\Fulltext\Action\DataProvider"> | |
+ <type name="Magento\CatalogSearch\Model\Indexer\Fulltext\Action\GetSearchableProductsSelect"> | |
<plugin name="stock_filters_by_child_products" type="Magento\InventoryCatalogSearch\Plugin\CatalogSearch\Model\Indexer\ChildProductFilterByInventoryStockPlugin"/> | |
</type> | |
</config> | |
-- | |
2.35.1 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment