Skip to content

Commit 3a460d2

Browse files
committed
MAGE-1083 Implement cache clean for attribute change by mass action
1 parent 9193cd2 commit 3a460d2

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

Helper/Entity/Product/CacheHelper.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Helper\Entity\Product;
4+
5+
use Algolia\AlgoliaSearch\Logger\AlgoliaLogger;
6+
use Algolia\AlgoliaSearch\Model\Cache\Product\IndexCollectionSize as Cache;
7+
8+
class CacheHelper
9+
{
10+
const ATTRIBUTES_TO_OBSERVE = ['status', 'visibility'];
11+
12+
public function __construct(
13+
protected Cache $cache,
14+
protected AlgoliaLogger $logger
15+
) {}
16+
17+
public function handleBulkAttributeChange(array $productIds, array $attributes, int $storeId)
18+
{
19+
if ($productIds
20+
&& array_intersect(array_keys($attributes), self::ATTRIBUTES_TO_OBSERVE)) {
21+
$this->logger->info(sprintf("Clearing product index collection cache on store ID %d for attributes: %s", $storeId, join(',', array_keys($attributes))));
22+
$this->cache->clear($storeId ?: null);
23+
}
24+
}
25+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Algolia\AlgoliaSearch\Plugin\Cache;
4+
5+
use Algolia\AlgoliaSearch\Helper\Entity\Product\CacheHelper;
6+
use Magento\Catalog\Controller\Adminhtml\Product\Action\Attribute\Save;
7+
use Magento\Catalog\Helper\Product\Edit\Action\Attribute;
8+
use Magento\Framework\Controller\Result\Redirect;
9+
10+
class CacheCleanBulkAttributePlugin
11+
{
12+
public function __construct(
13+
protected Attribute $attributeHelper,
14+
protected CacheHelper $cacheHelper
15+
) {}
16+
17+
/** In the event that the product_action_attribute.update consumer does not handle this change and update occurs in process
18+
* then this plugin will preemptively clear the cache
19+
*/
20+
public function afterExecute(
21+
Save $subject,
22+
Redirect $result
23+
): Redirect
24+
{
25+
$this->cacheHelper->handleBulkAttributeChange(
26+
$this->attributeHelper->getProductIds(),
27+
$subject->getRequest()->getParam('attributes', []),
28+
$this->attributeHelper->getSelectedStoreId()
29+
);
30+
31+
return $result;
32+
}
33+
}

Plugin/Cache/CacheCleanProductPlugin.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace Algolia\AlgoliaSearch\Plugin\Cache;
44

55
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
6+
use Algolia\AlgoliaSearch\Helper\Entity\Product\CacheHelper;
67
use Algolia\AlgoliaSearch\Model\Cache\Product\IndexCollectionSize as Cache;
78
use Magento\Catalog\Model\Product;
9+
use Magento\Catalog\Model\Product\Action;
810
use Magento\Catalog\Model\Product\Attribute\Source\Status;
911
use Magento\Catalog\Model\Product\Visibility;
1012
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
@@ -15,7 +17,8 @@ class CacheCleanProductPlugin
1517

1618
public function __construct(
1719
protected Cache $cache,
18-
protected ConfigHelper $configHelper
20+
protected ConfigHelper $configHelper,
21+
protected CacheHelper $cacheHelper
1922
) { }
2023

2124
public function beforeSave(ProductResource $subject, Product $product): void
@@ -47,6 +50,16 @@ public function afterDelete(ProductResource $subject, ProductResource $result):
4750
return $result;
4851
}
4952

53+
/**
54+
* Called on mass action "Change Status"
55+
* Called on "Update attributes" if `product_action_attribute.update` consumer is running
56+
*/
57+
public function afterUpdateAttributes(Action $subject, Action $result, array $productIds, array $attributes, int $storeId): Action
58+
{
59+
$this->cacheHelper->handleBulkAttributeChange($productIds, $attributes, $storeId);
60+
return $result;
61+
}
62+
5063
protected function isEligibleNewProduct(Product $product): bool
5164
{
5265
$storeId = $product->getStoreId();

etc/adminhtml/di.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@
1313
<argument name="frontendUrlBuilder" xsi:type="object">Magento\Framework\Url</argument>
1414
</arguments>
1515
</type>
16+
<type name="Magento\Catalog\Controller\Adminhtml\Product\Action\Attribute\Save">
17+
<plugin name="algolia_cache_clean_attribute_mass_action"
18+
type="\Algolia\AlgoliaSearch\Plugin\Cache\CacheCleanBulkAttributePlugin"
19+
sortOrder="1"
20+
/>
21+
</type>
1622
</config>

etc/di.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
</type>
77
<type name="Magento\Catalog\Model\Product\Action">
88
<plugin name="algoliaProductsMassAction" type="Algolia\AlgoliaSearch\Model\Indexer\ProductObserver"/>
9+
<plugin name="algoliaProductCacheCleanMassAction" type="Algolia\AlgoliaSearch\Plugin\Cache\CacheCleanProductPlugin"/>
910
</type>
11+
1012
<type name="Magento\CatalogInventory\Model\ResourceModel\Stock\Item">
1113
<plugin name="algoliaStockItems" type="Algolia\AlgoliaSearch\Plugin\StockItemObserver" disabled="true"/>
1214
</type>

0 commit comments

Comments
 (0)