@@ -18,6 +18,8 @@ class MissingPriceIndexHandler
18
18
public const PRICE_INDEX_TABLE_ALIAS = 'price_index ' ;
19
19
public const MAIN_TABLE_ALIAS = 'e ' ;
20
20
21
+ protected array $ _indexedProducts = [];
22
+
21
23
protected IndexerInterface $ indexer ;
22
24
public function __construct (
23
25
protected CollectionFactory $ productCollectionFactory ,
@@ -41,7 +43,7 @@ public function refreshPriceIndex(array|ProductCollection $products): array
41
43
return [];
42
44
}
43
45
44
- $ this ->logger ->log (__ ("Pricing records missing for %1 product(s) " , count ($ reindexIds )));
46
+ $ this ->logger ->log (__ ("Pricing records missing or invalid for %1 product(s) " , count ($ reindexIds )));
45
47
$ this ->logger ->log (__ ("Reindexing product ID(s): %1 " , implode (', ' , $ reindexIds )));
46
48
47
49
$ this ->indexer ->reindexList ($ reindexIds );
@@ -67,13 +69,18 @@ protected function getProductIdsToReindex(array|ProductCollection $products): ar
67
69
68
70
$ state = $ this ->indexer ->getState ()->getStatus ();
69
71
if ($ state === StateInterface::STATUS_INVALID ) {
70
- return $ productIds ; //all records must be reindexed
72
+ return $ this ->filterProductIdsNotYetProcessed ($ productIds );
73
+ }
74
+
75
+ $ productIds = $ this ->filterProductIdsMissingPricing ($ productIds );
76
+ if (empty ($ productIds )) {
77
+ return [];
71
78
}
72
79
73
- return $ this ->filterProductIds ($ productIds );
80
+ return $ this ->filterProductIdsNotYetProcessed ($ productIds );
74
81
}
75
82
76
- protected function filterProductIds (array $ productIds ): array
83
+ protected function filterProductIdsMissingPricing (array $ productIds ): array
77
84
{
78
85
$ collection = $ this ->productCollectionFactory ->create ();
79
86
@@ -92,6 +99,20 @@ protected function filterProductIds(array $productIds): array
92
99
return $ collection ->getAllIds ();
93
100
}
94
101
102
+ protected function filterProductIdsNotYetProcessed (array $ productIds ): array {
103
+ $ pendingProcessing = array_fill_keys ($ productIds , true );
104
+
105
+ $ notProcessed = array_diff_key ($ pendingProcessing , $ this ->_indexedProducts );
106
+
107
+ if (empty ($ notProcessed )) {
108
+ return [];
109
+ }
110
+
111
+ $ this ->_indexedProducts += $ notProcessed ;
112
+
113
+ return array_keys ($ notProcessed );
114
+ }
115
+
95
116
/**
96
117
* Expand the query for product ids from the collection regardless of price index status
97
118
* @throws \Zend_Db_Select_Exception
@@ -122,30 +143,6 @@ protected function expandPricingJoin(array &$joins, string $priceIndexJoin): voi
122
143
$ modifyJoin ['joinType ' ] = Zend_Db_Select::LEFT_JOIN ;
123
144
}
124
145
125
- /** Collection based approach - more memory intensive */
126
- protected function getProductIdsFromCollectionOrig (ProductCollection $ collection ): array
127
- {
128
- $ expandedCollection = clone $ collection ;
129
-
130
- $ select = $ expandedCollection ->getSelect ();
131
-
132
- $ joins = $ select ->getPart (Zend_Db_Select::FROM );
133
-
134
- $ priceIndexJoin = $ this ->getPriceIndexJoinAlias ($ joins );
135
-
136
- if (!$ priceIndexJoin ) {
137
- // nothing to do here - keep calm and carry on
138
- return [];
139
- }
140
-
141
- $ modifyJoin = &$ joins [$ priceIndexJoin ];
142
- $ modifyJoin ['joinType ' ] = Zend_Db_Select::LEFT_JOIN ;
143
-
144
- $ this ->rebuildJoins ($ select , $ joins );
145
-
146
- return $ expandedCollection ->getAllIds ();
147
- }
148
-
149
146
protected function rebuildJoins (Select $ select , array $ joins ): void
150
147
{
151
148
$ select ->reset (Zend_Db_Select::COLUMNS );
@@ -174,15 +171,6 @@ protected function rebuildJoins(Select $select, array $joins): void
174
171
}
175
172
}
176
173
177
- private function inspectJoins (array $ joins ): void {
178
- foreach ($ joins as $ alias => $ joinData ) {
179
- echo "Table Alias: $ alias \n" ;
180
- echo "Table Name: {$ joinData ['tableName ' ]}\n" ;
181
- echo "Join Type: {$ joinData ['joinType ' ]}\n" ;
182
- echo "Join Condition: " . ($ joinData ['joinCondition ' ] ?? 'N/A ' ) . "\n\n" ;
183
- }
184
- }
185
-
186
174
/**
187
175
* @param array<string, array> $joins
188
176
* @return string
0 commit comments