4
4
* Copyright © Magento, Inc. All rights reserved.
5
5
* See COPYING.txt for license details.
6
6
*/
7
+ declare (strict_types=1 );
7
8
8
9
namespace Magento \ConfigurableProduct \Plugin \Model \ResourceModel ;
9
10
10
11
use Magento \Catalog \Api \Data \ProductAttributeInterface ;
11
12
use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
13
+ use Magento \Catalog \Model \Product as ProductModel ;
14
+ use Magento \Catalog \Model \ResourceModel \Product as ProductResource ;
12
15
use Magento \ConfigurableProduct \Api \Data \OptionInterface ;
13
16
use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
14
17
use Magento \Framework \Api \FilterBuilder ;
15
18
use Magento \Framework \Api \SearchCriteriaBuilder ;
19
+ use Magento \Framework \App \CacheInterface ;
16
20
use Magento \Framework \App \ObjectManager ;
21
+ use Magento \Framework \DataObject ;
22
+ use Magento \Framework \Event \Manager as EventManager ;
17
23
use Magento \Framework \Indexer \ActionInterface ;
24
+ use Magento \Framework \Indexer \CacheContext ;
18
25
19
26
/**
20
27
* Plugin product resource model
28
+ *
29
+ * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
21
30
*/
22
31
class Product
23
32
{
@@ -46,21 +55,42 @@ class Product
46
55
*/
47
56
private $ filterBuilder ;
48
57
58
+ /**
59
+ * @var CacheContext
60
+ */
61
+ private $ cacheContext ;
62
+
63
+ /**
64
+ * @var EventManager
65
+ */
66
+ private $ eventManager ;
67
+
68
+ /**
69
+ * @var CacheInterface
70
+ */
71
+ private $ appCache ;
72
+
49
73
/**
50
74
* Initialize Product dependencies.
51
75
*
52
76
* @param Configurable $configurable
53
77
* @param ActionInterface $productIndexer
54
- * @param ProductAttributeRepositoryInterface $productAttributeRepository
55
- * @param SearchCriteriaBuilder $searchCriteriaBuilder
56
- * @param FilterBuilder $filterBuilder
78
+ * @param ProductAttributeRepositoryInterface|null $productAttributeRepository
79
+ * @param SearchCriteriaBuilder|null $searchCriteriaBuilder
80
+ * @param FilterBuilder|null $filterBuilder
81
+ * @param CacheContext|null $cacheContext
82
+ * @param EventManager|null $eventManager
83
+ * @param CacheInterface|null $appCache
57
84
*/
58
85
public function __construct (
59
86
Configurable $ configurable ,
60
87
ActionInterface $ productIndexer ,
61
88
ProductAttributeRepositoryInterface $ productAttributeRepository = null ,
62
- SearchCriteriaBuilder $ searchCriteriaBuilder = null ,
63
- FilterBuilder $ filterBuilder = null
89
+ ?SearchCriteriaBuilder $ searchCriteriaBuilder = null ,
90
+ ?FilterBuilder $ filterBuilder = null ,
91
+ ?CacheContext $ cacheContext = null ,
92
+ ?EventManager $ eventManager = null ,
93
+ ?CacheInterface $ appCache = null
64
94
) {
65
95
$ this ->configurable = $ configurable ;
66
96
$ this ->productIndexer = $ productIndexer ;
@@ -70,35 +100,65 @@ public function __construct(
70
100
->get (SearchCriteriaBuilder::class);
71
101
$ this ->filterBuilder = $ filterBuilder ?: ObjectManager::getInstance ()
72
102
->get (FilterBuilder::class);
103
+ $ this ->cacheContext = $ cacheContext ?? ObjectManager::getInstance ()->get (CacheContext::class);
104
+ $ this ->eventManager = $ eventManager ?? ObjectManager::getInstance ()->get (EventManager::class);
105
+ $ this ->appCache = $ appCache ?? ObjectManager::getInstance ()->get (CacheInterface::class);
73
106
}
74
107
75
108
/**
76
109
* We need reset attribute set id to attribute after related simple product was saved
77
110
*
78
- * @param \Magento\Catalog\Model\ResourceModel\Product $subject
79
- * @param \Magento\Framework\ DataObject $object
111
+ * @param ProductResource $subject
112
+ * @param DataObject $object
80
113
* @return void
81
- * @throws \Magento\Framework\Exception\NoSuchEntityException
82
114
*
83
115
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
84
116
*/
85
117
public function beforeSave (
86
- \ Magento \ Catalog \ Model \ ResourceModel \ Product $ subject ,
87
- \ Magento \ Framework \ DataObject $ object
118
+ ProductResource $ subject ,
119
+ DataObject $ object
88
120
) {
89
- /** @var \Magento\Catalog\Model\Product $object */
121
+ /** @var ProductModel $object */
90
122
if ($ object ->getTypeId () == Configurable::TYPE_CODE ) {
91
123
$ object ->getTypeInstance ()->getSetAttributes ($ object );
92
124
$ this ->resetConfigurableOptionsData ($ object );
93
125
}
94
126
}
95
127
128
+ /**
129
+ * Invalidate cache and perform reindexing for configurable associated product
130
+ *
131
+ * @param ProductResource $subject
132
+ * @param ProductResource $result
133
+ * @param DataObject $object
134
+ * @return ProductResource
135
+ *
136
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
137
+ */
138
+ public function afterSave (
139
+ ProductResource $ subject ,
140
+ ProductResource $ result ,
141
+ DataObject $ object
142
+ ): ProductResource {
143
+ $ configurableProductIds = $ this ->configurable ->getParentIdsByChild ($ object ->getId ());
144
+ if (count ($ configurableProductIds ) > 0 ) {
145
+ $ this ->cacheContext ->registerEntities (ProductModel::CACHE_TAG , $ configurableProductIds );
146
+ $ this ->eventManager ->dispatch ('clean_cache_by_tags ' , ['object ' => $ this ->cacheContext ]);
147
+ $ identities = $ this ->cacheContext ->getIdentities ();
148
+ if (!empty ($ identities )) {
149
+ $ this ->appCache ->clean ($ identities );
150
+ $ this ->cacheContext ->flush ();
151
+ }
152
+ }
153
+
154
+ return $ result ;
155
+ }
156
+
96
157
/**
97
158
* Set null for configurable options attribute of configurable product
98
159
*
99
- * @param \Magento\Catalog\Model\Product $object
160
+ * @param ProductModel $object
100
161
* @return void
101
- * @throws \Magento\Framework\Exception\NoSuchEntityException
102
162
*/
103
163
private function resetConfigurableOptionsData ($ object )
104
164
{
@@ -128,16 +188,16 @@ private function resetConfigurableOptionsData($object)
128
188
/**
129
189
* Gather configurable parent ids of product being deleted and reindex after delete is complete.
130
190
*
131
- * @param \Magento\Catalog\Model\ResourceModel\Product $subject
191
+ * @param ProductResource $subject
132
192
* @param \Closure $proceed
133
- * @param \Magento\Catalog\Model\Product $product
134
- * @return \Magento\Catalog\Model\ResourceModel\Product
193
+ * @param ProductModel $product
194
+ * @return ProductResource
135
195
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
136
196
*/
137
197
public function aroundDelete (
138
- \ Magento \ Catalog \ Model \ ResourceModel \ Product $ subject ,
198
+ ProductResource $ subject ,
139
199
\Closure $ proceed ,
140
- \ Magento \ Catalog \ Model \ Product $ product
200
+ ProductModel $ product
141
201
) {
142
202
$ configurableProductIds = $ this ->configurable ->getParentIdsByChild ($ product ->getId ());
143
203
$ result = $ proceed ($ product );
0 commit comments