9
9
10
10
use Magento \Catalog \Api \Data \ProductInterface ;
11
11
use Magento \Catalog \Model \Product ;
12
- use Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable \Product \CollectionFactory ;
13
- use Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable \Product \Collection as ChildCollection ;
14
12
use Magento \Catalog \Model \ProductFactory ;
13
+ use Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable \Product \Collection as ChildCollection ;
14
+ use Magento \ConfigurableProduct \Model \ResourceModel \Product \Type \Configurable \Product \CollectionFactory ;
15
15
use Magento \Framework \EntityManager \MetadataPool ;
16
16
use Magento \Framework \Api \SearchCriteriaBuilder ;
17
17
use Magento \CatalogGraphQl \Model \Resolver \Products \DataProvider \Product as DataProvider ;
@@ -47,9 +47,9 @@ class Collection
47
47
private $ metadataPool ;
48
48
49
49
/**
50
- * @var int []
50
+ * @var Product []
51
51
*/
52
- private $ parentIds = [];
52
+ private $ parentProducts = [];
53
53
54
54
/**
55
55
* @var array
@@ -83,19 +83,22 @@ public function __construct(
83
83
}
84
84
85
85
/**
86
- * Add parent Id to collection filter
86
+ * Add parent to collection filter
87
87
*
88
- * @param int $id
88
+ * @param Product $product
89
89
* @return void
90
90
*/
91
- public function addParentId ( int $ id ) : void
91
+ public function addParentProduct ( Product $ product ) : void
92
92
{
93
- if (!in_array ($ id , $ this ->parentIds ) && !empty ($ this ->childrenMap )) {
93
+ if (isset ($ this ->parentProducts [$ product ->getId ()])) {
94
+ return ;
95
+ }
96
+
97
+ if (!empty ($ this ->childrenMap )) {
94
98
$ this ->childrenMap = [];
95
- $ this ->parentIds [] = $ id ;
96
- } elseif (!in_array ($ id , $ this ->parentIds )) {
97
- $ this ->parentIds [] = $ id ;
99
+
98
100
}
101
+ $ this ->parentProducts [$ product ->getId ()] = $ product ;
99
102
}
100
103
101
104
/**
@@ -130,20 +133,23 @@ public function getChildProductsByParentId(int $id) : array
130
133
* Fetch all children products from parent id's.
131
134
*
132
135
* @return array
136
+ * @throws \Exception
133
137
*/
134
138
private function fetch () : array
135
139
{
136
- if (empty ($ this ->parentIds ) || !empty ($ this ->childrenMap )) {
140
+ if (empty ($ this ->parentProducts ) || !empty ($ this ->childrenMap )) {
137
141
return $ this ->childrenMap ;
138
142
}
139
143
140
144
$ linkField = $ this ->metadataPool ->getMetadata (ProductInterface::class)->getLinkField ();
141
- foreach ($ this ->parentIds as $ id ) {
145
+ foreach ($ this ->parentProducts as $ product ) {
146
+ $ attributeData = $ this ->getAttributesCodes ($ product );
142
147
/** @var ChildCollection $childCollection */
143
148
$ childCollection = $ this ->childCollectionFactory ->create ();
149
+ $ childCollection ->addAttributeToSelect ($ attributeData );
150
+
144
151
/** @var Product $product */
145
- $ product = $ this ->productFactory ->create ();
146
- $ product ->setData ($ linkField , $ id );
152
+ $ product ->setData ($ linkField , $ product ->getId ());
147
153
$ childCollection ->setProductFilter ($ product );
148
154
149
155
/** @var Product $childProduct */
@@ -160,4 +166,24 @@ private function fetch() : array
160
166
161
167
return $ this ->childrenMap ;
162
168
}
169
+
170
+ /**
171
+ * Get attributes code
172
+ *
173
+ * @param \Magento\Catalog\Model\Product $currentProduct
174
+ * @return array
175
+ */
176
+ private function getAttributesCodes (Product $ currentProduct ): array
177
+ {
178
+ $ attributeCodes = [];
179
+ $ allowAttributes = $ currentProduct ->getTypeInstance ()->getConfigurableAttributes ($ currentProduct );
180
+ foreach ($ allowAttributes as $ attribute ) {
181
+ $ productAttribute = $ attribute ->getProductAttribute ();
182
+ if (!\in_array ($ productAttribute ->getAttributeCode (), $ attributeCodes )) {
183
+ $ attributeCodes [] = $ productAttribute ->getAttributeCode ();
184
+ }
185
+ }
186
+
187
+ return $ attributeCodes ;
188
+ }
163
189
}
0 commit comments