Skip to content

Commit 4f6f42e

Browse files
committed
MAGE-1122 Add flexible retrieval of price reindex ids
1 parent 9bd63d9 commit 4f6f42e

File tree

2 files changed

+105
-8
lines changed

2 files changed

+105
-8
lines changed

Helper/Data.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,7 @@ public function rebuildStoreProductIndexPage(
688688
page ' . $page . ',
689689
pageSize ' . $pageSize;
690690
$this->logger->start($wrapperLogMessage);
691+
691692
if ($emulationInfo === null) {
692693
$this->startEmulation($storeId);
693694
}
@@ -713,6 +714,9 @@ public function rebuildStoreProductIndexPage(
713714
'store' => $storeId
714715
]
715716
);
717+
718+
$this->missingPriceIndexHandler->refreshPriceIndex($collection);
719+
716720
$logMessage = 'LOADING: ' . $this->logger->getStoreName($storeId) . ',
717721
collection page: ' . $page . ',
718722
pageSize: ' . $pageSize;

Service/Product/MissingPriceIndexHandler.php

Lines changed: 101 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22

33
namespace Algolia\AlgoliaSearch\Service\Product;
44

5+
use Magento\Framework\DB\Select;
6+
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
57
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
68
use Magento\Framework\Indexer\IndexerRegistry;
79
use Magento\Framework\Indexer\IndexerInterface;
10+
use Zend_Db_Select;
811

912
class MissingPriceIndexHandler
1013
{
14+
public const PRICE_INDEX_TABLE = 'catalog_product_index_price';
15+
public const PRICE_INDEX_TABLE_ALIAS = 'price_index';
16+
public const MAIN_TABLE_ALIAS = 'e';
17+
1118
protected IndexerInterface $indexer;
1219
public function __construct(
1320
protected CollectionFactory $productCollectionFactory,
@@ -18,12 +25,12 @@ public function __construct(
1825
}
1926

2027
/**
21-
* @param array $productIds
22-
* @return int[] Array of product IDs that were reindexed by this repair operation
28+
* @param string[]|ProductCollection $products
29+
* @return string[] Array of product IDs that were reindexed by this repair operation
2330
*/
24-
public function refreshPriceIndex(array $productIds): array
31+
public function refreshPriceIndex(array|ProductCollection $products): array
2532
{
26-
$reindexIds = $this->getProductIdsToReindex($productIds);
33+
$reindexIds = $this->getProductIdsToReindex($products);
2734
if (empty($reindexIds)) {
2835
return [];
2936
}
@@ -34,16 +41,25 @@ public function refreshPriceIndex(array $productIds): array
3441
}
3542

3643
/**
37-
* @param int[] $productIds
38-
* @return int[]
44+
* @param string[]|ProductCollection $products
45+
* @return string[]
3946
*/
40-
protected function getProductIdsToReindex(array $productIds): array
47+
protected function getProductIdsToReindex(array|ProductCollection $products): array
4148
{
49+
$productIds = $products instanceof ProductCollection
50+
? $this->getExpandedProductCollectionIds($products)
51+
: $products;
52+
4253
$state = $this->indexer->getState()->getStatus();
4354
if ($state === \Magento\Framework\Indexer\StateInterface::STATUS_INVALID) {
4455
return $productIds;
4556
}
4657

58+
return $this->filterProductIds($productIds);
59+
}
60+
61+
protected function filterProductIds(array $productIds): array
62+
{
4763
$collection = $this->productCollectionFactory->create();
4864

4965
$collection->addAttributeToSelect(['name', 'price']);
@@ -56,8 +72,85 @@ protected function getProductIdsToReindex(array $productIds): array
5672

5773
$collection->getSelect()
5874
->where('price_index.entity_id IS NULL')
59-
->where('entity_id IN (?)', $productIds);
75+
->where('e.entity_id IN (?)', $productIds);
6076

6177
return $collection->getAllIds();
6278
}
79+
80+
protected function getExpandedProductCollectionIds(ProductCollection $collection): array
81+
{
82+
$expandedCollection = clone $collection;
83+
84+
$select = $expandedCollection->getSelect();
85+
86+
$joins = $select->getPart(Zend_Db_Select::FROM);
87+
88+
$priceIndexJoin = $this->getPriceIndexJoinAlias($joins);
89+
90+
if (!$priceIndexJoin) {
91+
// nothing to do here - keep calm and carry on
92+
return [];
93+
}
94+
95+
$modifyJoin = &$joins[$priceIndexJoin];
96+
$modifyJoin['joinType'] = Zend_Db_Select::LEFT_JOIN;
97+
98+
$this->rebuildJoins($select, $joins);
99+
100+
return $expandedCollection->getAllIds();
101+
}
102+
103+
protected function rebuildJoins(Select $select, array $joins): void
104+
{
105+
$select->reset(Zend_Db_Select::FROM);
106+
foreach ($joins as $alias => $joinData) {
107+
if ($joinData['joinType'] === Zend_Db_Select::FROM) {
108+
$select->from([$alias => $joinData['tableName']]);
109+
} elseif ($joinData['joinType'] === Zend_Db_Select::LEFT_JOIN) {
110+
$select->joinLeft(
111+
[$alias => $joinData['tableName']],
112+
$joinData['joinCondition'],
113+
[],
114+
$joinData['schema']
115+
);
116+
} else {
117+
$select->join(
118+
[$alias => $joinData['tableName']],
119+
$joinData['joinCondition'],
120+
[],
121+
$joinData['schema']
122+
);
123+
}
124+
$sql = $select->__toString();
125+
}
126+
}
127+
128+
private function inspectJoins(array $joins): void {
129+
foreach ($joins as $alias => $joinData) {
130+
echo "Table Alias: $alias\n";
131+
echo "Table Name: {$joinData['tableName']}\n";
132+
echo "Join Type: {$joinData['joinType']}\n";
133+
echo "Join Condition: " . ($joinData['joinCondition'] ?? 'N/A') . "\n\n";
134+
}
135+
}
136+
137+
/**
138+
* @param array<string, array> $joins
139+
* @return string
140+
*/
141+
protected function getPriceIndexJoinAlias(array $joins): string
142+
{
143+
if (isset($joins[self::PRICE_INDEX_TABLE_ALIAS])) {
144+
return self::PRICE_INDEX_TABLE_ALIAS;
145+
}
146+
else {
147+
foreach ($joins as $alias => $joinData) {
148+
if ($joinData['tableName'] === self::PRICE_INDEX_TABLE) {
149+
return $alias;
150+
}
151+
}
152+
}
153+
154+
return "";
155+
}
63156
}

0 commit comments

Comments
 (0)