@@ -123,11 +123,12 @@ public function addEavAttributes(array $attributeCodes) : void
123123 *
124124 * @param int $id
125125 * @param ContextInterface $context
126+ * @param array $attributeCodes
126127 * @return array
127128 */
128- public function getChildProductsByParentId (int $ id , ContextInterface $ context ) : array
129+ public function getChildProductsByParentId (int $ id , ContextInterface $ context, array $ attributeCodes ) : array
129130 {
130- $ childrenMap = $ this ->fetch ($ context );
131+ $ childrenMap = $ this ->fetch ($ context, $ attributeCodes );
131132
132133 if (!isset ($ childrenMap [$ id ])) {
133134 return [];
@@ -140,67 +141,47 @@ public function getChildProductsByParentId(int $id, ContextInterface $context) :
140141 * Fetch all children products from parent id's.
141142 *
142143 * @param ContextInterface $context
144+ * @param array $attributeCodes
143145 * @return array
144146 */
145- private function fetch (ContextInterface $ context ) : array
147+ private function fetch (ContextInterface $ context, array $ attributeCodes ) : array
146148 {
147149 if (empty ($ this ->parentProducts ) || !empty ($ this ->childrenMap )) {
148150 return $ this ->childrenMap ;
149151 }
150152
153+ /** @var ChildCollection $childCollection */
154+ $ childCollection = $ this ->childCollectionFactory ->create ();
151155 foreach ($ this ->parentProducts as $ product ) {
152- $ attributeData = $ this ->getAttributesCodes ($ product );
153- /** @var ChildCollection $childCollection */
154- $ childCollection = $ this ->childCollectionFactory ->create ();
155156 $ 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- }
179157 }
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 ;
200174 }
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 ;
201182 }
202183
203- return $ attributeCodes ;
184+ return $ this -> childrenMap ;
204185 }
205186
206187 /**
0 commit comments