Skip to content

Commit edbe96e

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

File tree

5 files changed

+96
-32
lines changed

5 files changed

+96
-32
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogInventory\Model\ResourceModel;
9+
10+
/**
11+
* Search Result Applier getters and setters
12+
*/
13+
class StockStatusApplier implements StockStatusApplierInterface
14+
{
15+
/**
16+
* Storefront search result applier flag
17+
*
18+
* @var bool
19+
*/
20+
private $searchResultApplier = false;
21+
22+
/**
23+
* Set flag, if the request is originated from SearchResultApplier
24+
*
25+
* @param bool $status
26+
*/
27+
public function setSearchResultApplier(bool $status): void
28+
{
29+
$this->searchResultApplier = $status;
30+
}
31+
32+
/**
33+
* Get flag, if the request is originated from SearchResultApplier
34+
*
35+
* @return bool
36+
*/
37+
public function hasSearchResultApplier() : bool
38+
{
39+
return $this->searchResultApplier;
40+
}
41+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogInventory\Model\ResourceModel;
9+
10+
/**
11+
* Search Result Applier interface.
12+
*/
13+
interface StockStatusApplierInterface
14+
{
15+
16+
/**
17+
* Set flag, if the request is originated from SearchResultApplier
18+
*
19+
* @param bool $status
20+
*/
21+
public function setSearchResultApplier(bool $status): void;
22+
23+
/**
24+
* Get flag, if the request is originated from SearchResultApplier
25+
*
26+
* @return bool
27+
*/
28+
public function hasSearchResultApplier() : bool;
29+
}

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

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\CatalogInventory\Model\Stock;
1313
use Magento\Framework\App\ResourceConnection;
1414
use Magento\Framework\DB\Select;
15+
use Magento\Framework\App\ObjectManager;
1516

1617
/**
1718
* Generic in-stock status filter
@@ -20,13 +21,6 @@ class StockStatusFilter implements StockStatusFilterInterface
2021
{
2122
private const TABLE_NAME = 'cataloginventory_stock_status';
2223

23-
/**
24-
* Storefront search result applier flag
25-
*
26-
* @var bool
27-
*/
28-
private $searchResultApplier = false;
29-
3024
/**
3125
* @var ResourceConnection
3226
*/
@@ -37,36 +31,25 @@ class StockStatusFilter implements StockStatusFilterInterface
3731
*/
3832
private $stockConfiguration;
3933

34+
/**
35+
* @var StockStatusApplierInterface
36+
*/
37+
private $stockStatusApplier;
38+
4039
/**
4140
* @param ResourceConnection $resource
4241
* @param StockConfigurationInterface $stockConfiguration
42+
* @param StockStatusApplierInterface|null $stockStatusApplier
4343
*/
4444
public function __construct(
4545
ResourceConnection $resource,
46-
StockConfigurationInterface $stockConfiguration
46+
StockConfigurationInterface $stockConfiguration,
47+
?StockStatusApplierInterface $stockStatusApplier = null
4748
) {
4849
$this->resource = $resource;
4950
$this->stockConfiguration = $stockConfiguration;
50-
}
51-
52-
/**
53-
* Set flag, if the request is originated from SearchResultApplier
54-
*
55-
* @param bool $status
56-
*/
57-
public function setSearchResultApplier(bool $status): void
58-
{
59-
$this->searchResultApplier = $status;
60-
}
61-
62-
/**
63-
* Get flag, if the request is originated from SearchResultApplier
64-
*
65-
* @return bool
66-
*/
67-
public function hasSearchResultApplier() : bool
68-
{
69-
return $this->searchResultApplier;
51+
$this->stockStatusApplier = $stockStatusApplier
52+
?? ObjectManager::getInstance()->get(StockStatusApplierInterface::class);
7053
}
7154

7255
/**
@@ -96,7 +79,7 @@ public function execute(
9679
[]
9780
);
9881

99-
if ($this->hasSearchResultApplier()) {
82+
if ($this->stockStatusApplier->hasSearchResultApplier()) {
10083
$select->columns(["{$stockStatusTableAlias}.stock_status AS is_salable"]);
10184
} else {
10285
$select->where("{$stockStatusTableAlias}.stock_status = ?", StockStatusInterface::STATUS_IN_STOCK);

app/code/Magento/CatalogInventory/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
<preference for="Magento\CatalogInventory\Model\ResourceModel\QtyCounterInterface" type="Magento\CatalogInventory\Model\ResourceModel\Stock" />
3535
<preference for="Magento\CatalogInventory\Model\ResourceModel\StockStatusFilterInterface" type="Magento\CatalogInventory\Model\ResourceModel\StockStatusFilter" />
36+
<preference for="Magento\CatalogInventory\Model\ResourceModel\StockStatusApplierInterface" type="Magento\CatalogInventory\Model\ResourceModel\StockStatusApplier" />
3637
<type name="Magento\Catalog\Model\Product\Attribute\Repository">
3738
<plugin name="filterCustomAttribute" type="Magento\CatalogInventory\Model\Plugin\FilterCustomAttribute" />
3839
</type>

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

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

99
use Magento\Catalog\Api\Data\ProductInterface;
10+
use Magento\CatalogInventory\Model\ResourceModel\StockStatusApplierInterface;
1011
use Magento\CatalogInventory\Model\ResourceModel\StockStatusFilterInterface;
1112
use Magento\CatalogSearch\Model\ResourceModel\Fulltext\Collection\SearchResultApplierInterface;
1213
use Magento\Framework\Api\Search\SearchResultInterface;
@@ -57,6 +58,11 @@ class SearchResultApplier implements SearchResultApplierInterface
5758
*/
5859
private $stockStatusFilter;
5960

61+
/**
62+
* @var StockStatusApplierInterface
63+
*/
64+
private $stockStatusApplier;
65+
6066
/**
6167
* @param Collection $collection
6268
* @param SearchResultInterface $searchResult
@@ -65,6 +71,7 @@ class SearchResultApplier implements SearchResultApplierInterface
6571
* @param ScopeConfigInterface|null $scopeConfig
6672
* @param MetadataPool|null $metadataPool
6773
* @param StockStatusFilterInterface|null $stockStatusFilter
74+
* @param StockStatusApplierInterface|null $stockStatusApplier
6875
*/
6976
public function __construct(
7077
Collection $collection,
@@ -73,7 +80,8 @@ public function __construct(
7380
int $currentPage,
7481
?ScopeConfigInterface $scopeConfig = null,
7582
?MetadataPool $metadataPool = null,
76-
?StockStatusFilterInterface $stockStatusFilter = null
83+
?StockStatusFilterInterface $stockStatusFilter = null,
84+
?StockStatusApplierInterface $stockStatusApplier = null
7785
) {
7886
$this->collection = $collection;
7987
$this->searchResult = $searchResult;
@@ -82,7 +90,9 @@ public function __construct(
8290
$this->scopeConfig = $scopeConfig ?? ObjectManager::getInstance()->get(ScopeConfigInterface::class);
8391
$this->metadataPool = $metadataPool ?? ObjectManager::getInstance()->get(MetadataPool::class);
8492
$this->stockStatusFilter = $stockStatusFilter
85-
?: ObjectManager::getInstance()->get(StockStatusFilterInterface::class);
93+
?? ObjectManager::getInstance()->get(StockStatusFilterInterface::class);
94+
$this->stockStatusApplier = $stockStatusApplier
95+
?? ObjectManager::getInstance()->get(StockStatusApplierInterface::class);
8696
}
8797

8898
/**
@@ -212,7 +222,7 @@ private function categoryProductByCustomSortOrder(int $categoryId): array
212222
['e' => $this->collection->getTable('catalog_product_entity')],
213223
['e.entity_id']
214224
);
215-
$this->stockStatusFilter->setSearchResultApplier(true);
225+
$this->stockStatusApplier->setSearchResultApplier(true);
216226
$query = $this->stockStatusFilter->execute($query, 'e', 'stockItem');
217227
$query->join(
218228
['cat_index' => $this->collection->getTable('catalog_category_product_index_store' . $storeId)],

0 commit comments

Comments
 (0)