7
7
namespace Magento \Elasticsearch \Model \ResourceModel \Fulltext \Collection ;
8
8
9
9
use Magento \Catalog \Api \Data \ProductInterface ;
10
+ use Magento \CatalogInventory \Model \ResourceModel \StockStatusFilterInterface ;
10
11
use Magento \CatalogSearch \Model \ResourceModel \Fulltext \Collection \SearchResultApplierInterface ;
11
12
use Magento \Framework \Api \Search \SearchResultInterface ;
12
13
use Magento \Framework \App \Config \ScopeConfigInterface ;
13
14
use Magento \Framework \App \ObjectManager ;
14
15
use Magento \Framework \Data \Collection ;
15
16
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 ;
21
17
22
18
/**
23
19
* Resolve specific attributes for search criteria.
@@ -55,24 +51,9 @@ class SearchResultApplier implements SearchResultApplierInterface
55
51
private $ metadataPool ;
56
52
57
53
/**
58
- * @var StoreManagerInterface
54
+ * @var StockStatusFilterInterface
59
55
*/
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 ;
76
57
77
58
/**
78
59
* @param Collection $collection
@@ -81,11 +62,7 @@ class SearchResultApplier implements SearchResultApplierInterface
81
62
* @param int $currentPage
82
63
* @param ScopeConfigInterface|null $scopeConfig
83
64
* @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
89
66
*/
90
67
public function __construct (
91
68
Collection $ collection ,
@@ -94,21 +71,16 @@ public function __construct(
94
71
int $ currentPage ,
95
72
?ScopeConfigInterface $ scopeConfig = null ,
96
73
?MetadataPool $ metadataPool = null ,
97
- ?StoreManagerInterface $ storeManager = null ,
98
- ?StockByWebsiteIdResolverInterface $ stockByWebsiteIdResolver = null ,
99
- ?StockIndexTableNameResolverInterface $ stockIndexTableNameResolver = null ,
100
- ?DefaultStockProviderInterface $ defaultStockProvider = null
74
+ ?StockStatusFilterInterface $ stockStatusFilter = null
101
75
) {
102
76
$ this ->collection = $ collection ;
103
77
$ this ->searchResult = $ searchResult ;
104
78
$ this ->size = $ size ;
105
79
$ this ->currentPage = $ currentPage ;
106
80
$ this ->scopeConfig = $ scopeConfig ?? ObjectManager::getInstance ()->get (ScopeConfigInterface::class);
107
81
$ 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);
112
84
}
113
85
114
86
/**
@@ -223,9 +195,6 @@ private function getProductIdsBySaleability(): array
223
195
*/
224
196
private function categoryProductByCustomSortOrder (int $ categoryId ): array
225
197
{
226
- $ websiteId = $ this ->_storeManager ->getStore ()->getWebsiteId ();
227
- $ stock = $ this ->stockByWebsiteIdResolver ->execute ($ websiteId );
228
- $ stockTable = $ this ->stockIndexTableNameResolver ->execute ((int )$ stock ->getStockId ());
229
198
$ storeId = $ this ->collection ->getStoreId ();
230
199
$ searchCriteria = $ this ->searchResult ->getSearchCriteria ();
231
200
$ sortOrders = $ searchCriteria ->getSortOrders () ?? [];
@@ -242,20 +211,8 @@ private function categoryProductByCustomSortOrder(int $categoryId): array
242
211
['e ' => $ this ->collection ->getTable ('catalog_product_entity ' )],
243
212
['e.entity_id ' ]
244
213
);
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 ' );
259
216
$ query ->join (
260
217
['cat_index ' => $ this ->collection ->getTable ('catalog_category_product_index_store ' . $ storeId )],
261
218
'cat_index.product_id = e.entity_id '
@@ -310,15 +267,4 @@ private function hasShowOutOfStockStatus(): bool
310
267
\Magento \Store \Model \ScopeInterface::SCOPE_STORE
311
268
);
312
269
}
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
- }
324
270
}
0 commit comments