4
4
* Copyright © Magento, Inc. All rights reserved.
5
5
* See COPYING.txt for license details.
6
6
*/
7
+
7
8
namespace Magento \ConfigurableProduct \Plugin \Model \ResourceModel ;
8
9
9
10
use Magento \Catalog \Api \ProductAttributeRepositoryInterface ;
10
11
use Magento \ConfigurableProduct \Model \Product \Type \Configurable ;
11
12
use Magento \Framework \Indexer \ActionInterface ;
12
13
use Magento \ConfigurableProduct \Api \Data \OptionInterface ;
14
+ use Magento \Catalog \Api \Data \ProductAttributeInterface ;
13
15
14
16
/**
15
17
* Plugin product resource model
@@ -26,18 +28,42 @@ class Product
26
28
*/
27
29
private $ productIndexer ;
28
30
31
+ /**
32
+ * @var ProductAttributeRepositoryInterface
33
+ */
34
+ private $ productAttributeRepository ;
35
+
36
+ /**
37
+ * @var \Magento\Framework\Api\SearchCriteriaBuilder
38
+ */
39
+ private $ searchCriteriaBuilder ;
40
+
41
+ /**
42
+ * @var \Magento\Framework\Api\FilterBuilder
43
+ */
44
+ private $ filterBuilder ;
45
+
29
46
/**
30
47
* Initialize Product dependencies.
31
48
*
32
49
* @param Configurable $configurable
33
50
* @param ActionInterface $productIndexer
51
+ * @param ProductAttributeRepositoryInterface $productAttributeRepository
52
+ * @param \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder
53
+ * @param \Magento\Framework\Api\FilterBuilder $filterBuilder
34
54
*/
35
55
public function __construct (
36
56
Configurable $ configurable ,
37
- ActionInterface $ productIndexer
57
+ ActionInterface $ productIndexer ,
58
+ ProductAttributeRepositoryInterface $ productAttributeRepository ,
59
+ \Magento \Framework \Api \SearchCriteriaBuilder $ searchCriteriaBuilder ,
60
+ \Magento \Framework \Api \FilterBuilder $ filterBuilder
38
61
) {
39
62
$ this ->configurable = $ configurable ;
40
63
$ this ->productIndexer = $ productIndexer ;
64
+ $ this ->productAttributeRepository = $ productAttributeRepository ;
65
+ $ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
66
+ $ this ->filterBuilder = $ filterBuilder ;
41
67
}
42
68
43
69
/**
@@ -72,13 +98,23 @@ private function resetConfigurableOptionsData($object)
72
98
{
73
99
$ extensionAttribute = $ object ->getExtensionAttributes ();
74
100
if ($ extensionAttribute && $ extensionAttribute ->getConfigurableProductOptions ()) {
75
- /** @var ProductAttributeRepositoryInterface $productAttributeRepository */
76
- $ productAttributeRepository = \Magento \Framework \App \ObjectManager::getInstance ()
77
- ->get (ProductAttributeRepositoryInterface::class);
101
+ $ attributeIds = [];
78
102
/** @var OptionInterface $option */
79
103
foreach ($ extensionAttribute ->getConfigurableProductOptions () as $ option ) {
80
- $ eavAttribute = $ productAttributeRepository ->get ($ option ->getAttributeId ());
81
- $ object ->setData ($ eavAttribute ->getAttributeCode (), null );
104
+ $ attributeIds [] = $ option ->getAttributeId ();
105
+ }
106
+
107
+ $ filter = $ this ->filterBuilder
108
+ ->setField (ProductAttributeInterface::ATTRIBUTE_ID )
109
+ ->setConditionType ('in ' )
110
+ ->setValue ($ attributeIds )
111
+ ->create ();
112
+ $ this ->searchCriteriaBuilder ->addFilters ([$ filter ]);
113
+ $ searchCriteria = $ this ->searchCriteriaBuilder ->create ();
114
+ $ optionAttributes = $ this ->productAttributeRepository ->getList ($ searchCriteria )->getItems ();
115
+
116
+ foreach ($ optionAttributes as $ optionAttribute ) {
117
+ $ object ->setData ($ optionAttribute ->getAttributeCode (), null );
82
118
}
83
119
}
84
120
}
0 commit comments