@@ -123,11 +123,12 @@ public function addEavAttributes(array $attributeCodes) : void
123
123
*
124
124
* @param int $id
125
125
* @param ContextInterface $context
126
+ * @param array $attributeCodes
126
127
* @return array
127
128
*/
128
- public function getChildProductsByParentId (int $ id , ContextInterface $ context ) : array
129
+ public function getChildProductsByParentId (int $ id , ContextInterface $ context, array $ attributeCodes ) : array
129
130
{
130
- $ childrenMap = $ this ->fetch ($ context );
131
+ $ childrenMap = $ this ->fetch ($ context, $ attributeCodes );
131
132
132
133
if (!isset ($ childrenMap [$ id ])) {
133
134
return [];
@@ -140,67 +141,47 @@ public function getChildProductsByParentId(int $id, ContextInterface $context) :
140
141
* Fetch all children products from parent id's.
141
142
*
142
143
* @param ContextInterface $context
144
+ * @param array $attributeCodes
143
145
* @return array
144
146
*/
145
- private function fetch (ContextInterface $ context ) : array
147
+ private function fetch (ContextInterface $ context, array $ attributeCodes ) : array
146
148
{
147
149
if (empty ($ this ->parentProducts ) || !empty ($ this ->childrenMap )) {
148
150
return $ this ->childrenMap ;
149
151
}
150
152
153
+ /** @var ChildCollection $childCollection */
154
+ $ childCollection = $ this ->childCollectionFactory ->create ();
151
155
foreach ($ this ->parentProducts as $ product ) {
152
- $ attributeData = $ this ->getAttributesCodes ($ product );
153
- /** @var ChildCollection $childCollection */
154
- $ childCollection = $ this ->childCollectionFactory ->create ();
155
156
$ childCollection ->setProductFilter ($ product );
156
- $ childCollection ->addWebsiteFilter ($ context ->getExtensionAttributes ()->getStore ()->getWebsiteId ());
157
- $ this ->collectionProcessor ->process (
158
- $ childCollection ,
159
- $ this ->searchCriteriaBuilder ->create (),
160
- $ attributeData ,
161
- $ context
162
- );
163
- $ childCollection ->load ();
164
- $ this ->collectionPostProcessor ->process ($ childCollection , $ attributeData );
165
-
166
- /** @var Product $childProduct */
167
- foreach ($ childCollection as $ childProduct ) {
168
- if ((int )$ childProduct ->getStatus () !== Status::STATUS_ENABLED ) {
169
- continue ;
170
- }
171
- $ formattedChild = ['model ' => $ childProduct , 'sku ' => $ childProduct ->getSku ()];
172
- $ parentId = (int )$ childProduct ->getParentId ();
173
- if (!isset ($ this ->childrenMap [$ parentId ])) {
174
- $ this ->childrenMap [$ parentId ] = [];
175
- }
176
-
177
- $ this ->childrenMap [$ parentId ][] = $ formattedChild ;
178
- }
179
157
}
180
-
181
- return $ this ->childrenMap ;
182
- }
183
-
184
- /**
185
- * Get attributes codes for given product
186
- *
187
- * @param Product $currentProduct
188
- * @return array
189
- */
190
- private function getAttributesCodes (Product $ currentProduct ): array
191
- {
192
- $ attributeCodes = $ this ->attributeCodes ;
193
- if ($ currentProduct ->getTypeId () == Configurable::TYPE_CODE ) {
194
- $ allowAttributes = $ currentProduct ->getTypeInstance ()->getConfigurableAttributes ($ currentProduct );
195
- foreach ($ allowAttributes as $ attribute ) {
196
- $ productAttribute = $ attribute ->getProductAttribute ();
197
- if (!\in_array ($ productAttribute ->getAttributeCode (), $ attributeCodes )) {
198
- $ attributeCodes [] = $ productAttribute ->getAttributeCode ();
199
- }
158
+ $ childCollection ->addWebsiteFilter ($ context ->getExtensionAttributes ()->getStore ()->getWebsiteId ());
159
+
160
+ $ attributeCodes = array_unique (array_merge ($ this ->attributeCodes , $ attributeCodes ));
161
+
162
+ $ this ->collectionProcessor ->process (
163
+ $ childCollection ,
164
+ $ this ->searchCriteriaBuilder ->create (),
165
+ $ attributeCodes ,
166
+ $ context
167
+ );
168
+ $ this ->collectionPostProcessor ->process ($ childCollection , $ attributeCodes );
169
+
170
+ /** @var Product $childProduct */
171
+ foreach ($ childCollection as $ childProduct ) {
172
+ if ((int )$ childProduct ->getStatus () !== Status::STATUS_ENABLED ) {
173
+ continue ;
200
174
}
175
+ $ formattedChild = ['model ' => $ childProduct , 'sku ' => $ childProduct ->getSku ()];
176
+ $ parentId = (int )$ childProduct ->getParentId ();
177
+ if (!isset ($ this ->childrenMap [$ parentId ])) {
178
+ $ this ->childrenMap [$ parentId ] = [];
179
+ }
180
+
181
+ $ this ->childrenMap [$ parentId ][] = $ formattedChild ;
201
182
}
202
183
203
- return $ attributeCodes ;
184
+ return $ this -> childrenMap ;
204
185
}
205
186
206
187
/**
0 commit comments