Skip to content

Commit a521fcc

Browse files
authored
Merge pull request #1716 from algolia/fix/MAGE-1244-fix-affected-products
MAGE-1244: Fix `$affectedProductIds` logic
2 parents 45739ec + ce3b23f commit a521fcc

File tree

2 files changed

+29
-38
lines changed

2 files changed

+29
-38
lines changed

Model/Indexer/CategoryObserver.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@
22

33
namespace Algolia\AlgoliaSearch\Model\Indexer;
44

5+
use Algolia\AlgoliaSearch\Exception\DiagnosticsException;
56
use Algolia\AlgoliaSearch\Model\Indexer\Category as CategoryIndexer;
67
use Algolia\AlgoliaSearch\Service\AlgoliaCredentialsManager;
8+
use Algolia\AlgoliaSearch\Service\Product\BatchQueueProcessor as ProductBatchQueueProcessor;
79
use Magento\Catalog\Model\Category as CategoryModel;
810
use Magento\Catalog\Model\ResourceModel\Category as CategoryResourceModel;
911
use Magento\Catalog\Model\ResourceModel\Product\Collection as ProductCollection;
1012
use Magento\Framework\App\ResourceConnection;
13+
use Magento\Framework\Exception\NoSuchEntityException;
1114
use Magento\Framework\Indexer\IndexerRegistry;
15+
use Magento\Store\Model\StoreManagerInterface;
1216

1317
class CategoryObserver
1418
{
@@ -17,7 +21,9 @@ class CategoryObserver
1721

1822
public function __construct(
1923
IndexerRegistry $indexerRegistry,
24+
protected StoreManagerInterface $storeManager,
2025
protected ResourceConnection $resource,
26+
protected ProductBatchQueueProcessor $productBatchQueueProcessor,
2127
protected AlgoliaCredentialsManager $algoliaCredentialsManager
2228
) {
2329
$this->indexer = $indexerRegistry->get('algolia_categories');
@@ -52,8 +58,8 @@ public function afterSave(
5258
$changedProductIds = ($category->getChangedProductIds() !== null ? (array) $category->getChangedProductIds() : []);
5359

5460
if (!$this->indexer->isScheduled()) {
55-
CategoryIndexer::$affectedProductIds = array_unique(array_merge($changedProductIds, $collectionIds));
5661
$this->indexer->reindexRow($category->getId());
62+
$this->reindexAffectedProducts(array_unique(array_merge($changedProductIds, $collectionIds)));
5763
} else {
5864
// missing logic, if scheduled, when category is saved w/out product, products need to be added to _cl
5965
if (count($changedProductIds) === 0 && count($collectionIds) > 0) {
@@ -82,15 +88,29 @@ public function afterDelete(
8288
if (!$this->indexer->isScheduled()) {
8389
/* we are using products position because getProductCollection() doesn't use correct store */
8490
$productCollection = $category->getProductsPosition();
85-
CategoryIndexer::$affectedProductIds = array_keys($productCollection);
86-
8791
$this->indexer->reindexRow($category->getId());
92+
$this->reindexAffectedProducts(array_keys($productCollection));
8893
}
8994
});
9095

9196
return $result;
9297
}
9398

99+
/**
100+
* @param array $affectedProductIds
101+
* @return void
102+
* @throws DiagnosticsException
103+
* @throws NoSuchEntityException
104+
*/
105+
protected function reindexAffectedProducts(array $affectedProductIds): void
106+
{
107+
if (count($affectedProductIds) > 0) {
108+
foreach (array_keys($this->storeManager->getStores()) as $storeId) {
109+
$this->productBatchQueueProcessor->processBatch($storeId, $affectedProductIds);
110+
}
111+
}
112+
}
113+
94114
/**
95115
* @param array $productIds
96116
*/

Service/Category/BatchQueueProcessor.php

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@
1010
use Algolia\AlgoliaSearch\Model\Queue;
1111
use Algolia\AlgoliaSearch\Service\AlgoliaCredentialsManager;
1212
use Algolia\AlgoliaSearch\Service\Category\IndexBuilder as CategoryIndexBuilder;
13-
use Algolia\AlgoliaSearch\Service\Product\IndexBuilder as ProductIndexBuilder;
13+
use Magento\Framework\Exception\LocalizedException;
1414
use Magento\Framework\Exception\NoSuchEntityException;
1515

1616
class BatchQueueProcessor implements BatchQueueProcessorInterface
1717
{
18-
public static $affectedProductIds = [];
19-
2018
public function __construct(
2119
protected Data $dataHelper,
2220
protected ConfigHelper $configHelper,
@@ -29,7 +27,7 @@ public function __construct(
2927
* @param int $storeId
3028
* @param array|null $entityIds
3129
* @return void
32-
* @throws NoSuchEntityException
30+
* @throws NoSuchEntityException|LocalizedException
3331
*/
3432
public function processBatch(int $storeId, ?array $entityIds = null): void
3533
{
@@ -43,8 +41,6 @@ public function processBatch(int $storeId, ?array $entityIds = null): void
4341
return;
4442
}
4543

46-
$this->rebuildAffectedProducts($storeId);
47-
4844
$categoriesPerPage = $this->configHelper->getNumberOfElementByPage();
4945

5046
if (is_array($entityIds) && count($entityIds) > 0) {
@@ -56,37 +52,12 @@ public function processBatch(int $storeId, ?array $entityIds = null): void
5652
$this->processFullReindex($storeId, $categoriesPerPage);
5753
}
5854

59-
/**
60-
* @param int $storeId
61-
*/
62-
protected function rebuildAffectedProducts($storeId)
63-
{
64-
$affectedProducts = self::$affectedProductIds;
65-
$affectedProductsCount = count($affectedProducts);
66-
67-
if ($affectedProductsCount > 0 && $this->configHelper->indexProductOnCategoryProductsUpdate($storeId)) {
68-
$productsPerPage = $this->configHelper->getNumberOfElementByPage();
69-
foreach (array_chunk($affectedProducts, $productsPerPage) as $chunk) {
70-
/** @uses ProductIndexBuilder::buildIndexList() */
71-
$this->queue->addToQueue(
72-
ProductIndexBuilder::class,
73-
'buildIndexList',
74-
[
75-
'storeId' => $storeId,
76-
'entityIds' => $chunk,
77-
],
78-
count($chunk)
79-
);
80-
}
81-
}
82-
}
83-
8455
/**
8556
* @param array $categoryIds
8657
* @param int $categoriesPerPage
8758
* @param int $storeId
8859
*/
89-
protected function processSpecificCategories($categoryIds, $categoriesPerPage, $storeId)
60+
protected function processSpecificCategories(array $categoryIds, int $categoriesPerPage, int $storeId): void
9061
{
9162
foreach (array_chunk($categoryIds, $categoriesPerPage) as $chunk) {
9263
/** @uses CategoryIndexBuilder::buildIndexList */
@@ -106,10 +77,10 @@ protected function processSpecificCategories($categoryIds, $categoriesPerPage, $
10677
* @param int $storeId
10778
* @param int $categoriesPerPage
10879
*
109-
* @throws Magento\Framework\Exception\LocalizedException
110-
* @throws Magento\Framework\Exception\NoSuchEntityException
80+
* @throws NoSuchEntityException
81+
* @throws LocalizedException
11182
*/
112-
protected function processFullReindex($storeId, $categoriesPerPage)
83+
protected function processFullReindex(int $storeId, int $categoriesPerPage): void
11384
{
11485
/** @uses IndicesConfigurator::saveConfigurationToAlgolia() */
11586
$this->queue->addToQueue(IndicesConfigurator::class, 'saveConfigurationToAlgolia', ['storeId' => $storeId]);

0 commit comments

Comments
 (0)