15
15
*/
16
16
class InvalidateCacheOnCategoryDesignChange implements ObserverInterface
17
17
{
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
+
18
33
/**
19
34
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
35
+ * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
20
36
*/
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
+ ) {
23
41
$ 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
+ ];
24
60
}
25
61
26
62
/**
@@ -33,7 +69,7 @@ public function execute(Observer $observer)
33
69
$ category = $ observer ->getEvent ()->getEntity ();
34
70
if (!$ category ->isObjectNew ()) {
35
71
foreach ($ category ->getDesignAttributes () as $ designAttribute ) {
36
- if ($ category -> dataHasChangedFor ($ designAttribute ->getAttributeCode ())) {
72
+ if ($ this -> isCategoryAttributeChanged ($ designAttribute ->getAttributeCode (), $ category )) {
37
73
$ this ->cacheTypeList ->invalidate (
38
74
[
39
75
\Magento \PageCache \Model \Cache \Type::TYPE_IDENTIFIER ,
@@ -45,4 +81,42 @@ public function execute(Observer $observer)
45
81
}
46
82
}
47
83
}
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
+ }
48
122
}
0 commit comments