|
17 | 17 | use Magento\Store\Model\StoreManagerInterface;
|
18 | 18 | use Magento\Framework\DB\Adapter\AdapterInterface;
|
19 | 19 | use Magento\Framework\Indexer\IndexerRegistry;
|
| 20 | +use Magento\Catalog\Model\Indexer\Product\Category as ProductCategoryIndexer; |
20 | 21 | use Magento\Catalog\Model\Indexer\Category\Product as CategoryProductIndexer;
|
21 | 22 | use Magento\Catalog\Model\Indexer\Category\Product\TableMaintainer;
|
22 | 23 | use Magento\Indexer\Model\WorkingStateProvider;
|
@@ -104,46 +105,68 @@ public function execute(array $entityIds = [], $useTempTable = false)
|
104 | 105 | $this->limitationByProducts = $idsToBeReIndexed;
|
105 | 106 | $this->useTempTable = $useTempTable;
|
106 | 107 | $indexer = $this->indexerRegistry->get(CategoryProductIndexer::INDEXER_ID);
|
107 |
| - $workingState = $this->workingStateProvider->isWorking($indexer->getId()); |
| 108 | + $workingState = $this->getWorkingState(); |
108 | 109 |
|
109 |
| - $affectedCategories = $this->getCategoryIdsFromIndex($idsToBeReIndexed); |
| 110 | + if (!$indexer->isScheduled() |
| 111 | + || ($indexer->isScheduled() && !$useTempTable) |
| 112 | + || ($indexer->isScheduled() && $useTempTable && !$workingState)) { |
110 | 113 |
|
111 |
| - if ($useTempTable && !$workingState && $indexer->isScheduled()) { |
112 |
| - foreach ($this->storeManager->getStores() as $store) { |
113 |
| - $this->connection->truncateTable($this->getIndexTable($store->getId())); |
| 114 | + $affectedCategories = $this->getCategoryIdsFromIndex($idsToBeReIndexed); |
| 115 | + |
| 116 | + if ($useTempTable && !$workingState && $indexer->isScheduled()) { |
| 117 | + foreach ($this->storeManager->getStores() as $store) { |
| 118 | + $this->connection->truncateTable($this->getIndexTable($store->getId())); |
| 119 | + } |
| 120 | + } else { |
| 121 | + $this->removeEntries(); |
114 | 122 | }
|
115 |
| - } else { |
116 |
| - $this->removeEntries(); |
117 |
| - } |
118 |
| - $this->reindex(); |
119 |
| - if ($useTempTable && !$workingState && $indexer->isScheduled()) { |
120 |
| - foreach ($this->storeManager->getStores() as $store) { |
121 |
| - $this->connection->delete( |
122 |
| - $this->tableMaintainer->getMainTable($store->getId()), |
123 |
| - ['product_id IN (?)' => $this->limitationByProducts] |
124 |
| - ); |
125 |
| - $select = $this->connection->select() |
126 |
| - ->from($this->tableMaintainer->getMainReplicaTable($store->getId())); |
127 |
| - $this->connection->query( |
128 |
| - $this->connection->insertFromSelect( |
129 |
| - $select, |
| 123 | + $this->reindex(); |
| 124 | + |
| 125 | + // get actual state |
| 126 | + $workingState = $this->getWorkingState(); |
| 127 | + |
| 128 | + if ($useTempTable && !$workingState && $indexer->isScheduled()) { |
| 129 | + foreach ($this->storeManager->getStores() as $store) { |
| 130 | + $this->connection->delete( |
130 | 131 | $this->tableMaintainer->getMainTable($store->getId()),
|
131 |
| - [], |
132 |
| - AdapterInterface::INSERT_ON_DUPLICATE |
133 |
| - ) |
134 |
| - ); |
| 132 | + ['product_id IN (?)' => $this->limitationByProducts] |
| 133 | + ); |
| 134 | + $select = $this->connection->select() |
| 135 | + ->from($this->tableMaintainer->getMainReplicaTable($store->getId())); |
| 136 | + $this->connection->query( |
| 137 | + $this->connection->insertFromSelect( |
| 138 | + $select, |
| 139 | + $this->tableMaintainer->getMainTable($store->getId()), |
| 140 | + [], |
| 141 | + AdapterInterface::INSERT_ON_DUPLICATE |
| 142 | + ) |
| 143 | + ); |
| 144 | + } |
135 | 145 | }
|
136 |
| - } |
137 | 146 |
|
138 |
| - $affectedCategories = array_merge($affectedCategories, $this->getCategoryIdsFromIndex($idsToBeReIndexed)); |
| 147 | + $affectedCategories = array_merge($affectedCategories, $this->getCategoryIdsFromIndex($idsToBeReIndexed)); |
139 | 148 |
|
140 |
| - $this->registerProducts($idsToBeReIndexed); |
141 |
| - $this->registerCategories($affectedCategories); |
142 |
| - $this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->cacheContext]); |
| 149 | + $this->registerProducts($idsToBeReIndexed); |
| 150 | + $this->registerCategories($affectedCategories); |
| 151 | + $this->eventManager->dispatch('clean_cache_by_tags', ['object' => $this->cacheContext]); |
| 152 | + } |
143 | 153 |
|
144 | 154 | return $this;
|
145 | 155 | }
|
146 | 156 |
|
| 157 | + /** |
| 158 | + * Get state for current and shared indexer |
| 159 | + * |
| 160 | + * @return bool |
| 161 | + */ |
| 162 | + private function getWorkingState() : bool |
| 163 | + { |
| 164 | + $indexer = $this->indexerRegistry->get(CategoryProductIndexer::INDEXER_ID); |
| 165 | + $sharedIndexer = $this->indexerRegistry->get(ProductCategoryIndexer::INDEXER_ID); |
| 166 | + return $this->workingStateProvider->isWorking($indexer->getId()) |
| 167 | + || $this->workingStateProvider->isWorking($sharedIndexer->getId()); |
| 168 | + } |
| 169 | + |
147 | 170 | /**
|
148 | 171 | * Get IDs of parent products by their child IDs.
|
149 | 172 | *
|
|
0 commit comments