Skip to content

Commit b8d0a54

Browse files
committed
ACP2E-1524: Page Cache is not cleared for the parent product on the child product save
1 parent 1c874bd commit b8d0a54

File tree

1 file changed

+73
-18
lines changed
  • app/code/Magento/ConfigurableProduct/Plugin/Model/ResourceModel

1 file changed

+73
-18
lines changed

app/code/Magento/ConfigurableProduct/Plugin/Model/ResourceModel/Product.php

Lines changed: 73 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@
44
* Copyright © Magento, Inc. All rights reserved.
55
* See COPYING.txt for license details.
66
*/
7+
declare(strict_types=1);
78

89
namespace Magento\ConfigurableProduct\Plugin\Model\ResourceModel;
910

1011
use Magento\Catalog\Api\Data\ProductAttributeInterface;
1112
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
13+
use Magento\Catalog\Model\Indexer\Product\Category;
14+
use Magento\Catalog\Model\Product as ProductModel;
15+
use Magento\Catalog\Model\ResourceModel\Product as ProductResource;
16+
use Magento\CatalogSearch\Model\Indexer\Fulltext as FulltextIndexer;
1217
use Magento\ConfigurableProduct\Api\Data\OptionInterface;
1318
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
1419
use Magento\Framework\Api\FilterBuilder;
1520
use Magento\Framework\Api\SearchCriteriaBuilder;
1621
use Magento\Framework\App\ObjectManager;
22+
use Magento\Framework\DataObject;
1723
use Magento\Framework\Indexer\ActionInterface;
24+
use Magento\Framework\Indexer\CacheContext;
25+
use Magento\Framework\Indexer\IndexerRegistry;
1826

1927
/**
2028
* Plugin product resource model
@@ -46,21 +54,35 @@ class Product
4654
*/
4755
private $filterBuilder;
4856

57+
/**
58+
* @var CacheContext
59+
*/
60+
private $cacheContext;
61+
62+
/**
63+
* @var IndexerRegistry
64+
*/
65+
private $indexerRegistry;
66+
4967
/**
5068
* Initialize Product dependencies.
5169
*
5270
* @param Configurable $configurable
5371
* @param ActionInterface $productIndexer
54-
* @param ProductAttributeRepositoryInterface $productAttributeRepository
55-
* @param SearchCriteriaBuilder $searchCriteriaBuilder
56-
* @param FilterBuilder $filterBuilder
72+
* @param ProductAttributeRepositoryInterface|null $productAttributeRepository
73+
* @param SearchCriteriaBuilder|null $searchCriteriaBuilder
74+
* @param FilterBuilder|null $filterBuilder
75+
* @param CacheContext|null $cacheContext
76+
* @param IndexerRegistry|null $indexerRegistry
5777
*/
5878
public function __construct(
5979
Configurable $configurable,
6080
ActionInterface $productIndexer,
6181
ProductAttributeRepositoryInterface $productAttributeRepository = null,
62-
SearchCriteriaBuilder $searchCriteriaBuilder = null,
63-
FilterBuilder $filterBuilder = null
82+
?SearchCriteriaBuilder $searchCriteriaBuilder = null,
83+
?FilterBuilder $filterBuilder = null,
84+
?CacheContext $cacheContext = null,
85+
?IndexerRegistry $indexerRegistry = null
6486
) {
6587
$this->configurable = $configurable;
6688
$this->productIndexer = $productIndexer;
@@ -70,35 +92,68 @@ public function __construct(
7092
->get(SearchCriteriaBuilder::class);
7193
$this->filterBuilder = $filterBuilder ?: ObjectManager::getInstance()
7294
->get(FilterBuilder::class);
95+
$this->cacheContext = $cacheContext ?: ObjectManager::getInstance()->get(CacheContext::class);
96+
$this->indexerRegistry = $indexerRegistry ?: ObjectManager::getInstance()
97+
->get(IndexerRegistry::class);
7398
}
7499

75100
/**
76101
* We need reset attribute set id to attribute after related simple product was saved
77102
*
78-
* @param \Magento\Catalog\Model\ResourceModel\Product $subject
79-
* @param \Magento\Framework\DataObject $object
103+
* @param ProductResource $subject
104+
* @param DataObject $object
80105
* @return void
81-
* @throws \Magento\Framework\Exception\NoSuchEntityException
82106
*
83107
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
84108
*/
85109
public function beforeSave(
86-
\Magento\Catalog\Model\ResourceModel\Product $subject,
87-
\Magento\Framework\DataObject $object
110+
ProductResource $subject,
111+
DataObject $object
88112
) {
89-
/** @var \Magento\Catalog\Model\Product $object */
113+
/** @var ProductModel $object */
90114
if ($object->getTypeId() == Configurable::TYPE_CODE) {
91115
$object->getTypeInstance()->getSetAttributes($object);
92116
$this->resetConfigurableOptionsData($object);
93117
}
94118
}
95119

120+
/**
121+
* Invalidate cache and perform reindexing for configurable associated product
122+
*
123+
* @param ProductResource $subject
124+
* @param ProductResource $result
125+
* @param DataObject $object
126+
* @return ProductResource
127+
*
128+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
129+
*/
130+
public function afterSave(
131+
ProductResource $subject,
132+
ProductResource $result,
133+
DataObject $object
134+
): ProductResource {
135+
$productId = $object->getId();
136+
$parentProductIds = $this->configurable->getParentIdsByChild($productId);
137+
if (count($parentProductIds) > 0) {
138+
$productCategoryIndexer = $this->indexerRegistry->get(Category::INDEXER_ID);
139+
$productCategoryIndexer->reindexRow($productId);
140+
141+
$this->cacheContext->registerEntities(
142+
ProductModel::CACHE_TAG,
143+
array_unique(array_merge([$productId], $parentProductIds))
144+
);
145+
$indexer = $this->indexerRegistry->get(FulltextIndexer::INDEXER_ID);
146+
$indexer->reindexRow($productId);
147+
}
148+
149+
return $result;
150+
}
151+
96152
/**
97153
* Set null for configurable options attribute of configurable product
98154
*
99-
* @param \Magento\Catalog\Model\Product $object
155+
* @param ProductModel $object
100156
* @return void
101-
* @throws \Magento\Framework\Exception\NoSuchEntityException
102157
*/
103158
private function resetConfigurableOptionsData($object)
104159
{
@@ -128,16 +183,16 @@ private function resetConfigurableOptionsData($object)
128183
/**
129184
* Gather configurable parent ids of product being deleted and reindex after delete is complete.
130185
*
131-
* @param \Magento\Catalog\Model\ResourceModel\Product $subject
186+
* @param ProductResource $subject
132187
* @param \Closure $proceed
133-
* @param \Magento\Catalog\Model\Product $product
134-
* @return \Magento\Catalog\Model\ResourceModel\Product
188+
* @param ProductModel $product
189+
* @return ProductResource
135190
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
136191
*/
137192
public function aroundDelete(
138-
\Magento\Catalog\Model\ResourceModel\Product $subject,
193+
ProductResource $subject,
139194
\Closure $proceed,
140-
\Magento\Catalog\Model\Product $product
195+
ProductModel $product
141196
) {
142197
$configurableProductIds = $this->configurable->getParentIdsByChild($product->getId());
143198
$result = $proceed($product);

0 commit comments

Comments
 (0)