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 \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 ;
12
17
use Magento \ConfigurableProduct \Api \Data \OptionInterface ;
13
18
use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
14
19
use Magento \Framework \Api \FilterBuilder ;
15
20
use Magento \Framework \Api \SearchCriteriaBuilder ;
16
21
use Magento \Framework \App \ObjectManager ;
22
+ use Magento \Framework \DataObject ;
17
23
use Magento \Framework \Indexer \ActionInterface ;
24
+ use Magento \Framework \Indexer \CacheContext ;
25
+ use Magento \Framework \Indexer \IndexerRegistry ;
18
26
19
27
/**
20
28
* Plugin product resource model
@@ -46,21 +54,35 @@ class Product
46
54
*/
47
55
private $ filterBuilder ;
48
56
57
+ /**
58
+ * @var CacheContext
59
+ */
60
+ private $ cacheContext ;
61
+
62
+ /**
63
+ * @var IndexerRegistry
64
+ */
65
+ private $ indexerRegistry ;
66
+
49
67
/**
50
68
* Initialize Product dependencies.
51
69
*
52
70
* @param Configurable $configurable
53
71
* @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
57
77
*/
58
78
public function __construct (
59
79
Configurable $ configurable ,
60
80
ActionInterface $ productIndexer ,
61
81
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
64
86
) {
65
87
$ this ->configurable = $ configurable ;
66
88
$ this ->productIndexer = $ productIndexer ;
@@ -70,35 +92,68 @@ public function __construct(
70
92
->get (SearchCriteriaBuilder::class);
71
93
$ this ->filterBuilder = $ filterBuilder ?: ObjectManager::getInstance ()
72
94
->get (FilterBuilder::class);
95
+ $ this ->cacheContext = $ cacheContext ?: ObjectManager::getInstance ()->get (CacheContext::class);
96
+ $ this ->indexerRegistry = $ indexerRegistry ?: ObjectManager::getInstance ()
97
+ ->get (IndexerRegistry::class);
73
98
}
74
99
75
100
/**
76
101
* We need reset attribute set id to attribute after related simple product was saved
77
102
*
78
- * @param \Magento\Catalog\Model\ResourceModel\Product $subject
79
- * @param \Magento\Framework\ DataObject $object
103
+ * @param ProductResource $subject
104
+ * @param DataObject $object
80
105
* @return void
81
- * @throws \Magento\Framework\Exception\NoSuchEntityException
82
106
*
83
107
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
84
108
*/
85
109
public function beforeSave (
86
- \ Magento \ Catalog \ Model \ ResourceModel \ Product $ subject ,
87
- \ Magento \ Framework \ DataObject $ object
110
+ ProductResource $ subject ,
111
+ DataObject $ object
88
112
) {
89
- /** @var \Magento\Catalog\Model\Product $object */
113
+ /** @var ProductModel $object */
90
114
if ($ object ->getTypeId () == Configurable::TYPE_CODE ) {
91
115
$ object ->getTypeInstance ()->getSetAttributes ($ object );
92
116
$ this ->resetConfigurableOptionsData ($ object );
93
117
}
94
118
}
95
119
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
+
96
152
/**
97
153
* Set null for configurable options attribute of configurable product
98
154
*
99
- * @param \Magento\Catalog\Model\Product $object
155
+ * @param ProductModel $object
100
156
* @return void
101
- * @throws \Magento\Framework\Exception\NoSuchEntityException
102
157
*/
103
158
private function resetConfigurableOptionsData ($ object )
104
159
{
@@ -128,16 +183,16 @@ private function resetConfigurableOptionsData($object)
128
183
/**
129
184
* Gather configurable parent ids of product being deleted and reindex after delete is complete.
130
185
*
131
- * @param \Magento\Catalog\Model\ResourceModel\Product $subject
186
+ * @param ProductResource $subject
132
187
* @param \Closure $proceed
133
- * @param \Magento\Catalog\Model\Product $product
134
- * @return \Magento\Catalog\Model\ResourceModel\Product
188
+ * @param ProductModel $product
189
+ * @return ProductResource
135
190
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
136
191
*/
137
192
public function aroundDelete (
138
- \ Magento \ Catalog \ Model \ ResourceModel \ Product $ subject ,
193
+ ProductResource $ subject ,
139
194
\Closure $ proceed ,
140
- \ Magento \ Catalog \ Model \ Product $ product
195
+ ProductModel $ product
141
196
) {
142
197
$ configurableProductIds = $ this ->configurable ->getParentIdsByChild ($ product ->getId ());
143
198
$ result = $ proceed ($ product );
0 commit comments