Skip to content

Commit 04497c2

Browse files
committed
MC-42791: [Magento Cloud] Products not showing in catagories
1 parent 63e49d3 commit 04497c2

File tree

2 files changed

+300
-99
lines changed

2 files changed

+300
-99
lines changed

app/code/Magento/CatalogSearch/Model/Indexer/Fulltext/Action/DataProvider.php

Lines changed: 15 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@
55
*/
66
namespace Magento\CatalogSearch\Model\Indexer\Fulltext\Action;
77

8+
use Exception;
89
use Magento\Catalog\Api\Data\ProductInterface;
910
use Magento\Catalog\Model\Product;
10-
use Magento\Catalog\Model\Product\Attribute\Source\Status;
1111
use Magento\Catalog\Model\Product\Type;
1212
use Magento\Catalog\Model\ResourceModel\Product\Attribute\CollectionFactory;
1313
use Magento\CatalogSearch\Model\ResourceModel\EngineInterface;
1414
use Magento\CatalogSearch\Model\ResourceModel\EngineProvider;
1515
use Magento\Eav\Model\Config;
1616
use Magento\Eav\Model\Entity\Attribute;
17+
use Magento\Framework\App\ObjectManager;
1718
use Magento\Framework\App\ResourceConnection;
1819
use Magento\Framework\DataObject;
1920
use Magento\Framework\DB\Adapter\AdapterInterface;
2021
use Magento\Framework\DB\Select;
2122
use Magento\Framework\EntityManager\EntityMetadata;
2223
use Magento\Framework\EntityManager\MetadataPool;
2324
use Magento\Framework\Event\ManagerInterface;
24-
use Magento\Store\Model\Store;
2525
use Magento\Store\Model\StoreManagerInterface;
2626
use Zend_Db;
2727

@@ -138,6 +138,11 @@ class DataProvider
138138
*/
139139
private $antiGapMultiplier;
140140

141+
/**
142+
* @var GetSearchableProductsSelect|mixed
143+
*/
144+
private $selectSearchableProducts;
145+
141146
/**
142147
* @param ResourceConnection $resource
143148
* @param Type $catalogProductType
@@ -148,6 +153,8 @@ class DataProvider
148153
* @param StoreManagerInterface $storeManager
149154
* @param MetadataPool $metadataPool
150155
* @param int $antiGapMultiplier
156+
* @param GetSearchableProductsSelect|null $getSearchableProductsSelect
157+
* @throws Exception
151158
*/
152159
public function __construct(
153160
ResourceConnection $resource,
@@ -158,7 +165,8 @@ public function __construct(
158165
ManagerInterface $eventManager,
159166
StoreManagerInterface $storeManager,
160167
MetadataPool $metadataPool,
161-
int $antiGapMultiplier = 5
168+
int $antiGapMultiplier = 5,
169+
GetSearchableProductsSelect $getSearchableProductsSelect = null
162170
) {
163171
$this->resource = $resource;
164172
$this->connection = $resource->getConnection();
@@ -170,6 +178,7 @@ public function __construct(
170178
$this->engine = $engineProvider->get();
171179
$this->metadata = $metadataPool->getMetadata(ProductInterface::class);
172180
$this->antiGapMultiplier = $antiGapMultiplier;
181+
$this->selectSearchableProducts = $getSearchableProductsSelect ?: ObjectManager::getInstance()->get(GetSearchableProductsSelect::class);
173182
}
174183

175184
/**
@@ -201,8 +210,8 @@ public function getSearchableProducts(
201210
$lastProductId = 0,
202211
$batch = 100
203212
) {
204-
// $batch = 10;
205-
$select = $this->getSelectForSearchableProducts($storeId, $staticFields, $productIds, $lastProductId, $batch);
213+
$batch = 10;
214+
$select = $this->selectSearchableProducts->execute($storeId, $staticFields, $productIds, $lastProductId, $batch);
206215
if ($productIds === null) {
207216
$select->where(
208217
'e.entity_id < ?',
@@ -213,106 +222,13 @@ public function getSearchableProducts(
213222
if ($productIds === null && !$products) {
214223
// try to search without limit entity_id by batch size for cover case with a big gap between entity ids
215224
$products = $this->connection->fetchAll(
216-
$this->getSelectForSearchableProducts($storeId, $staticFields, $productIds, $lastProductId, $batch)
225+
$this->selectSearchableProducts->execute($storeId, $staticFields, $productIds, $lastProductId, $batch)
217226
);
218227
}
219228

220229
return $products;
221230
}
222231

223-
/**
224-
* Get Select object for searchable products
225-
*
226-
* @param int $storeId
227-
* @param array $staticFields
228-
* @param array|int $productIds
229-
* @param int $lastProductId
230-
* @param int $batch
231-
* @return Select
232-
*/
233-
public function getSelectForSearchableProducts(
234-
$storeId,
235-
array $staticFields,
236-
$productIds,
237-
$lastProductId,
238-
$batch
239-
) {
240-
$websiteId = (int)$this->storeManager->getStore($storeId)->getWebsiteId();
241-
$lastProductId = (int)$lastProductId;
242-
243-
$select = $this->connection->select()
244-
->useStraightJoin(true)
245-
->from(
246-
['e' => $this->getTable('catalog_product_entity')],
247-
array_merge(['entity_id', 'type_id'], $staticFields)
248-
)
249-
->join(
250-
['website' => $this->getTable('catalog_product_website')],
251-
$this->connection->quoteInto('website.product_id = e.entity_id AND website.website_id = ?', $websiteId),
252-
[]
253-
);
254-
255-
$this->joinAttribute($select, 'visibility', $storeId, $this->engine->getAllowedVisibility());
256-
$this->joinAttribute($select, 'status', $storeId, [Status::STATUS_ENABLED]);
257-
258-
if ($productIds !== null) {
259-
$select->where('e.entity_id IN (?)', $productIds, Zend_Db::INT_TYPE);
260-
}
261-
$select->where('e.entity_id > ?', $lastProductId);
262-
$select->order('e.entity_id');
263-
$select->limit($batch);
264-
265-
return $select;
266-
}
267-
268-
/**
269-
* Join attribute to searchable product for filtration
270-
*
271-
* @param Select $select
272-
* @param string $attributeCode
273-
* @param int $storeId
274-
* @param array $whereValue
275-
*/
276-
private function joinAttribute(Select $select, $attributeCode, $storeId, array $whereValue)
277-
{
278-
$linkField = $this->metadata->getLinkField();
279-
$attribute = $this->getSearchableAttribute($attributeCode);
280-
$attributeTable = $this->getTable('catalog_product_entity_' . $attribute->getBackendType());
281-
$defaultAlias = $attributeCode . '_default';
282-
$storeAlias = $attributeCode . '_store';
283-
284-
$whereCondition = $this->connection->getCheckSql(
285-
$storeAlias . '.value_id > 0',
286-
$storeAlias . '.value',
287-
$defaultAlias . '.value'
288-
);
289-
290-
$select->join(
291-
[$defaultAlias => $attributeTable],
292-
$this->connection->quoteInto(
293-
$defaultAlias . '.' . $linkField . '= e.' . $linkField . ' AND ' . $defaultAlias . '.attribute_id = ?',
294-
$attribute->getAttributeId()
295-
) . $this->connection->quoteInto(
296-
' AND ' . $defaultAlias . '.store_id = ?',
297-
Store::DEFAULT_STORE_ID
298-
),
299-
[]
300-
)->joinLeft(
301-
[$storeAlias => $attributeTable],
302-
$this->connection->quoteInto(
303-
$storeAlias . '.' . $linkField . '= e.' . $linkField . ' AND ' . $storeAlias . '.attribute_id = ?',
304-
$attribute->getAttributeId()
305-
) . $this->connection->quoteInto(
306-
' AND ' . $storeAlias . '.store_id = ?',
307-
$storeId
308-
),
309-
[]
310-
)->where(
311-
$whereCondition . ' IN (?)',
312-
$whereValue
313-
);
314-
}
315-
316232
/**
317233
* Retrieve searchable attributes
318234
*

0 commit comments

Comments
 (0)