@@ -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,24 @@ 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
+
147
+ $ attributeData = $ this ->getAttributesCodes ($ product );
142
148
/** @var ChildCollection $childCollection */
143
149
$ childCollection = $ this ->childCollectionFactory ->create ();
150
+ $ childCollection ->addAttributeToSelect ($ attributeData );
151
+
144
152
/** @var Product $product */
145
- $ product = $ this ->productFactory ->create ();
146
- $ product ->setData ($ linkField , $ id );
153
+ $ product ->setData ($ linkField , $ product ->getId ());
147
154
$ childCollection ->setProductFilter ($ product );
148
155
149
156
/** @var Product $childProduct */
@@ -160,4 +167,28 @@ private function fetch() : array
160
167
161
168
return $ this ->childrenMap ;
162
169
}
170
+
171
+ /**
172
+ * Get attributes codes
173
+ *
174
+ * @param Product $currentProduct
175
+ * @return array
176
+ */
177
+ private function getAttributesCodes (Product $ currentProduct ): array
178
+ {
179
+ $ attributeCodes = [];
180
+ $ attributes = $ currentProduct ->getAttributes ();
181
+ foreach ($ attributes as $ key => $ attribute ) {
182
+ $ isVisible = (int )$ attribute ->getIsVisibleOnFront ();
183
+ if (!$ isVisible ) {
184
+ continue ;
185
+ }
186
+ if (!in_array ($ key , $ attributeCodes )) {
187
+ continue ;
188
+ }
189
+ $ attributeCodes [] = $ key ;
190
+ }
191
+
192
+ return $ attributeCodes ;
193
+ }
163
194
}
0 commit comments