Skip to content

Commit 603ed51

Browse files
committed
ACP2E-322:'Move out of stock to the bottom' automating sorting is not updated when product's stock changes
1 parent 7bd1743 commit 603ed51

File tree

2 files changed

+38
-64
lines changed

2 files changed

+38
-64
lines changed

app/code/Magento/CatalogInventory/Model/ResourceModel/StockStatusFilter.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
class StockStatusFilter implements StockStatusFilterInterface
2020
{
2121
private const TABLE_NAME = 'cataloginventory_stock_status';
22+
23+
/**
24+
* @var bool|null
25+
*/
26+
private ?bool $searchResultApplier = null;
27+
2228
/**
2329
* @var ResourceConnection
2430
*/
@@ -41,6 +47,26 @@ public function __construct(
4147
$this->stockConfiguration = $stockConfiguration;
4248
}
4349

50+
/**
51+
* Set flag, if the request is originated from SearchResultApplier
52+
*
53+
* @param bool $status
54+
*/
55+
public function setSearchResultApplier(bool $status): void
56+
{
57+
$this->searchResultApplier = $status;
58+
}
59+
60+
/**
61+
* Get flag, if the request is originated from SearchResultApplier
62+
*
63+
* @return bool
64+
*/
65+
public function getSearchResultApplier() : bool
66+
{
67+
return $this->searchResultApplier;
68+
}
69+
4470
/**
4571
* @inheritDoc
4672
*/
@@ -67,7 +93,9 @@ public function execute(
6793
implode(' AND ', $joinCondition),
6894
[]
6995
);
70-
$select->where("{$stockStatusTableAlias}.stock_status = ?", StockStatusInterface::STATUS_IN_STOCK);
96+
if (!$this->getSearchResultApplier()) {
97+
$select->where("{$stockStatusTableAlias}.stock_status = ?", StockStatusInterface::STATUS_IN_STOCK);
98+
}
7199

72100
return $select;
73101
}

app/code/Magento/Elasticsearch/Model/ResourceModel/Fulltext/Collection/SearchResultApplier.php

Lines changed: 9 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,13 @@
77
namespace Magento\Elasticsearch\Model\ResourceModel\Fulltext\Collection;
88

99
use Magento\Catalog\Api\Data\ProductInterface;
10+
use Magento\CatalogInventory\Model\ResourceModel\StockStatusFilterInterface;
1011
use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\SearchResultApplierInterface;
1112
use Magento\Framework\Api\Search\SearchResultInterface;
1213
use Magento\Framework\App\Config\ScopeConfigInterface;
1314
use Magento\Framework\App\ObjectManager;
1415
use Magento\Framework\Data\Collection;
1516
use Magento\Framework\EntityManager\MetadataPool;
16-
use Magento\InventoryApi\Api\Data\StockInterface;
17-
use Magento\InventoryCatalogApi\Api\DefaultStockProviderInterface;
18-
use Magento\InventoryIndexer\Model\StockIndexTableNameResolverInterface;
19-
use Magento\InventorySalesApi\Model\StockByWebsiteIdResolverInterface;
20-
use Magento\Store\Model\StoreManagerInterface;
2117

2218
/**
2319
* Resolve specific attributes for search criteria.
@@ -55,24 +51,9 @@ class SearchResultApplier implements SearchResultApplierInterface
5551
private $metadataPool;
5652

5753
/**
58-
* @var StoreManagerInterface
54+
* @var StockStatusFilterInterface
5955
*/
60-
protected $_storeManager;
61-
62-
/**
63-
* @var StockByWebsiteIdResolverInterface
64-
*/
65-
private $stockByWebsiteIdResolver;
66-
67-
/**
68-
* @var StockIndexTableNameResolverInterface|mixed
69-
*/
70-
private $stockIndexTableNameResolver;
71-
72-
/**
73-
* @var DefaultStockProviderInterface
74-
*/
75-
private $defaultStockProvider;
56+
private $stockStatusFilter;
7657

7758
/**
7859
* @param Collection $collection
@@ -81,11 +62,7 @@ class SearchResultApplier implements SearchResultApplierInterface
8162
* @param int $currentPage
8263
* @param ScopeConfigInterface|null $scopeConfig
8364
* @param MetadataPool|null $metadataPool
84-
* @param StoreManagerInterface|null $storeManager
85-
* @param StockByWebsiteIdResolverInterface|null $stockByWebsiteIdResolver
86-
* @param StockIndexTableNameResolverInterface|null $stockIndexTableNameResolver
87-
* @param DefaultStockProviderInterface|null $defaultStockProvider
88-
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
65+
* @param StockStatusFilterInterface|null $stockStatusFilter
8966
*/
9067
public function __construct(
9168
Collection $collection,
@@ -94,21 +71,16 @@ public function __construct(
9471
int $currentPage,
9572
?ScopeConfigInterface $scopeConfig = null,
9673
?MetadataPool $metadataPool = null,
97-
?StoreManagerInterface $storeManager = null,
98-
?StockByWebsiteIdResolverInterface $stockByWebsiteIdResolver = null,
99-
?StockIndexTableNameResolverInterface $stockIndexTableNameResolver = null,
100-
?DefaultStockProviderInterface $defaultStockProvider = null
74+
?StockStatusFilterInterface $stockStatusFilter = null
10175
) {
10276
$this->collection = $collection;
10377
$this->searchResult = $searchResult;
10478
$this->size = $size;
10579
$this->currentPage = $currentPage;
10680
$this->scopeConfig = $scopeConfig ?? ObjectManager::getInstance()->get(ScopeConfigInterface::class);
10781
$this->metadataPool = $metadataPool ?? ObjectManager::getInstance()->get(MetadataPool::class);
108-
$this->_storeManager = $storeManager ?? ObjectManager::getInstance()->get(StoreManagerInterface::class);
109-
$this->stockByWebsiteIdResolver = $stockByWebsiteIdResolver ?? ObjectManager::getInstance()->get(StockByWebsiteIdResolverInterface::class);
110-
$this->stockIndexTableNameResolver = $stockIndexTableNameResolver ?? ObjectManager::getInstance()->get(StockIndexTableNameResolverInterface::class);
111-
$this->defaultStockProvider = $defaultStockProvider ?? ObjectManager::getInstance()->get(DefaultStockProviderInterface::class);
82+
$this->stockStatusFilter = $stockStatusFilter
83+
?: ObjectManager::getInstance()->get(StockStatusFilterInterface::class);
11284
}
11385

11486
/**
@@ -223,9 +195,6 @@ private function getProductIdsBySaleability(): array
223195
*/
224196
private function categoryProductByCustomSortOrder(int $categoryId): array
225197
{
226-
$websiteId = $this->_storeManager->getStore()->getWebsiteId();
227-
$stock = $this->stockByWebsiteIdResolver->execute($websiteId);
228-
$stockTable = $this->stockIndexTableNameResolver->execute((int)$stock->getStockId());
229198
$storeId = $this->collection->getStoreId();
230199
$searchCriteria = $this->searchResult->getSearchCriteria();
231200
$sortOrders = $searchCriteria->getSortOrders() ?? [];
@@ -242,20 +211,8 @@ private function categoryProductByCustomSortOrder(int $categoryId): array
242211
['e' => $this->collection->getTable('catalog_product_entity')],
243212
['e.entity_id']
244213
);
245-
246-
if (!$this->isDefaultStock($stock)) {
247-
$query->join(
248-
['inventory_stock' => $stockTable],
249-
'inventory_stock.sku = e.sku',
250-
['inventory_stock.is_salable']
251-
);
252-
} else {
253-
$query->join(
254-
['stock_status_index' => $this->collection->getTable('cataloginventory_stock_status')],
255-
'stock_status_index.product_id = e.entity_id',
256-
['stock_status_index.stock_status AS is_salable']
257-
);
258-
}
214+
$this->stockStatusFilter->setSearchResultApplier(true);
215+
$query = $this->stockStatusFilter->execute($query, 'e', 'stockItem');
259216
$query->join(
260217
['cat_index' => $this->collection->getTable('catalog_category_product_index_store' . $storeId)],
261218
'cat_index.product_id = e.entity_id'
@@ -310,15 +267,4 @@ private function hasShowOutOfStockStatus(): bool
310267
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
311268
);
312269
}
313-
314-
/**
315-
* Checks if inventory stock is DB view
316-
*
317-
* @param StockInterface $stock
318-
* @return bool
319-
*/
320-
private function isDefaultStock(StockInterface $stock): bool
321-
{
322-
return (int)$stock->getStockId() === $this->defaultStockProvider->getId();
323-
}
324270
}

0 commit comments

Comments
 (0)