@@ -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 $ parentProduct = [];
53
53
54
54
/**
55
55
* @var array
@@ -83,18 +83,18 @@ 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 addParentId (Product $ product ) : void
92
92
{
93
- if (!in_array ($ id , $ this ->parentIds ) && !empty ($ this ->childrenMap )) {
93
+ if (!in_array ($ product , $ this ->parentProduct ) && !empty ($ this ->childrenMap )) {
94
94
$ this ->childrenMap = [];
95
- $ this ->parentIds [] = $ id ;
96
- } elseif (!in_array ($ id , $ this ->parentIds )) {
97
- $ this ->parentIds [] = $ id ;
95
+ $ this ->parentProduct [] = $ product ;
96
+ } elseif (!in_array ($ product , $ this ->parentProduct )) {
97
+ $ this ->parentProduct [] = $ product ;
98
98
}
99
99
}
100
100
@@ -130,20 +130,24 @@ public function getChildProductsByParentId(int $id) : array
130
130
* Fetch all children products from parent id's.
131
131
*
132
132
* @return array
133
+ * @throws \Exception
133
134
*/
134
135
private function fetch () : array
135
136
{
136
- if (empty ($ this ->parentIds ) || !empty ($ this ->childrenMap )) {
137
+ if (empty ($ this ->parentProduct ) || !empty ($ this ->childrenMap )) {
137
138
return $ this ->childrenMap ;
138
139
}
139
140
140
141
$ linkField = $ this ->metadataPool ->getMetadata (ProductInterface::class)->getLinkField ();
141
- foreach ($ this ->parentIds as $ id ) {
142
+ foreach ($ this ->parentProduct as $ product ) {
143
+
144
+ $ attributeData = $ this ->getAttributesCode ($ product );
142
145
/** @var ChildCollection $childCollection */
143
146
$ childCollection = $ this ->childCollectionFactory ->create ();
147
+ $ childCollection ->addAttributeToSelect ($ attributeData );
148
+
144
149
/** @var Product $product */
145
- $ product = $ this ->productFactory ->create ();
146
- $ product ->setData ($ linkField , $ id );
150
+ $ product ->setData ($ linkField , $ product ->getId ());
147
151
$ childCollection ->setProductFilter ($ product );
148
152
149
153
/** @var Product $childProduct */
@@ -160,4 +164,24 @@ private function fetch() : array
160
164
161
165
return $ this ->childrenMap ;
162
166
}
167
+
168
+ /**
169
+ * Get attributes code
170
+ *
171
+ * @param \Magento\Catalog\Model\Product $currentProduct
172
+ * @return array
173
+ */
174
+ private function getAttributesCode (Product $ currentProduct ): array
175
+ {
176
+ $ attributeCode = [];
177
+ $ allowAttributes = $ currentProduct ->getTypeInstance ()->getConfigurableAttributes ($ currentProduct );
178
+ foreach ($ allowAttributes as $ attribute ) {
179
+ $ productAttribute = $ attribute ->getProductAttribute ();
180
+ if (!\in_array ($ productAttribute ->getAttributeCode (), $ attributeCode )) {
181
+ $ attributeCode [] = $ productAttribute ->getAttributeCode ();
182
+ }
183
+ }
184
+
185
+ return $ attributeCode ;
186
+ }
163
187
}
0 commit comments