Skip to content

Commit a06c378

Browse files
committed
MAGETWO-45666: "Refresh cache" message is not displayed when changing category page layout
- Fixed wrong design change detection when default category layout is changed
1 parent 7619a1b commit a06c378

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

app/code/Magento/Catalog/Model/CategoryRepository.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ public function save(\Magento\Catalog\Api\Data\CategoryInterface $category)
107107
$parentCategory = $this->get($parentId, $storeId);
108108
$existingData['path'] = $parentCategory->getPath();
109109
$existingData['parent_id'] = $parentId;
110-
$existingData['custom_apply_to_products'] = $existingData['custom_apply_to_products'] ?? 0;
111-
$existingData['custom_use_parent_settings'] = $existingData['custom_use_parent_settings'] ?? 0;
112110
}
113111
$category->addData($existingData);
114112
try {

app/code/Magento/Catalog/Observer/InvalidateCacheOnCategoryDesignChange.php

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,48 @@
1515
*/
1616
class InvalidateCacheOnCategoryDesignChange implements ObserverInterface
1717
{
18+
/**
19+
* Default category design attributes values
20+
*/
21+
private $defaultAttributeValues;
22+
23+
/**
24+
* @var \Magento\Framework\App\Cache\TypeListInterface
25+
*/
26+
private $cacheTypeList;
27+
28+
/**
29+
* @var \Magento\Framework\App\Config\ScopeConfigInterface
30+
*/
31+
private $scopeConfig;
32+
1833
/**
1934
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
35+
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
2036
*/
21-
public function __construct(\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList)
22-
{
37+
public function __construct(
38+
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
39+
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
40+
) {
2341
$this->cacheTypeList = $cacheTypeList;
42+
$this->scopeConfig = $scopeConfig;
43+
}
44+
45+
/**
46+
* Get default category design attribute values
47+
*
48+
* @return array
49+
*/
50+
private function getDefaultAttributeValues()
51+
{
52+
return [
53+
'custom_apply_to_products' => '0',
54+
'custom_use_parent_settings' => '0',
55+
'page_layout' => $this->scopeConfig->getValue(
56+
'web/default_layouts/default_category_layout',
57+
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
58+
)
59+
];
2460
}
2561

2662
/**
@@ -33,7 +69,7 @@ public function execute(Observer $observer)
3369
$category = $observer->getEvent()->getEntity();
3470
if (!$category->isObjectNew()) {
3571
foreach ($category->getDesignAttributes() as $designAttribute) {
36-
if ($category->dataHasChangedFor($designAttribute->getAttributeCode())) {
72+
if ($this->isCategoryAttributeChanged($designAttribute->getAttributeCode(), $category)) {
3773
$this->cacheTypeList->invalidate(
3874
[
3975
\Magento\PageCache\Model\Cache\Type::TYPE_IDENTIFIER,
@@ -45,4 +81,42 @@ public function execute(Observer $observer)
4581
}
4682
}
4783
}
84+
85+
/**
86+
* Check if category attribute changed
87+
*
88+
* @param string $attributeCode
89+
* @param \Magento\Catalog\Api\Data\CategoryInterface $category
90+
* @return bool
91+
*/
92+
private function isCategoryAttributeChanged($attributeCode, $category)
93+
{
94+
if (!array_key_exists($attributeCode, $category->getOrigData())) {
95+
$defaultValue = $this->getDefaultAttributeValue($attributeCode);
96+
if ($category->getData($attributeCode) !== $defaultValue) {
97+
return true;
98+
}
99+
} else {
100+
if ($category->dataHasChangedFor($attributeCode)) {
101+
return true;
102+
}
103+
}
104+
105+
return false;
106+
}
107+
108+
/**
109+
* Get default category design attribute value
110+
*
111+
* @param string $attributeCode
112+
* @return mixed|null
113+
*/
114+
private function getDefaultAttributeValue($attributeCode)
115+
{
116+
if ($this->defaultAttributeValues === null) {
117+
$this->defaultAttributeValues = $this->getDefaultAttributeValues();
118+
}
119+
120+
return $this->defaultAttributeValues[$attributeCode] ?? null;
121+
}
48122
}

0 commit comments

Comments
 (0)