10
10
use Exception ;
11
11
use Magento \Catalog \Api \Data \ProductInterface ;
12
12
use Magento \Catalog \Model \Product ;
13
- use Magento \Catalog \Model \ResourceModel \Product \Collection as ChildCollection ;
14
- use Magento \Catalog \Model \ResourceModel \Product \CollectionFactory ;
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 \Framework \ObjectManager \ResetAfterRequestInterface ;
@@ -126,7 +126,6 @@ public function addEavAttributes(array $attributeCodes) : void
126
126
* @param ContextInterface $context
127
127
* @param array $attributeCodes
128
128
* @return array
129
- * @throws Exception
130
129
*/
131
130
public function getChildProductsByParentId (int $ id , ContextInterface $ context , array $ attributeCodes ) : array
132
131
{
@@ -155,18 +154,16 @@ private function fetch(ContextInterface $context, array $attributeCodes) : array
155
154
156
155
/** @var ChildCollection $childCollection */
157
156
$ childCollection = $ this ->childCollectionFactory ->create ();
158
- $ linkField = $ this ->metadataPool ->getMetadata (ProductInterface::class)->getLinkField ();
157
+ foreach ($ this ->parentProducts as $ product ) {
158
+ $ childCollection ->setProductFilter ($ product );
159
+ }
159
160
$ childCollection ->addWebsiteFilter ($ context ->getExtensionAttributes ()->getStore ()->getWebsiteId ());
160
- $ childCollection ->getSelect ()
161
- ->columns (
162
- ['parent_id ' => new \Zend_Db_Expr ('GROUP_CONCAT(`link_table`.parent_id) ' )]
163
- )
164
- ->join (
165
- ['link_table ' => $ childCollection ->getTable ('catalog_product_super_link ' )],
166
- 'link_table.product_id = e.entity_id ' ,
167
- []
168
- )
169
- ->group ('e. ' . $ linkField );
161
+ $ linkField = $ this ->metadataPool ->getMetadata (ProductInterface::class)->getLinkField ();
162
+ $ childCollection ->getSelect ()->group ('e. ' . $ linkField );
163
+ $ childCollection ->getSelect ()->columns ([
164
+ 'parent_ids ' => new \Zend_Db_Expr ('GROUP_CONCAT(link_table.parent_id) ' )
165
+ ]);
166
+
170
167
$ attributeCodes = array_unique (array_merge ($ this ->attributeCodes , $ attributeCodes ));
171
168
172
169
$ this ->collectionProcessor ->process (
@@ -183,8 +180,7 @@ private function fetch(ContextInterface $context, array $attributeCodes) : array
183
180
continue ;
184
181
}
185
182
$ formattedChild = ['model ' => $ childProduct , 'sku ' => $ childProduct ->getSku ()];
186
-
187
- $ parentIds = preg_split ("/\,/ " , (string ) $ childProduct ->getParentId ());
183
+ $ parentIds = $ childProduct ->getParentIds () ? explode (', ' , $ childProduct ->getParentIds ()) : [];
188
184
foreach ($ parentIds as $ parentId ) {
189
185
if (!isset ($ this ->childrenMap [$ parentId ])) {
190
186
$ this ->childrenMap [$ parentId ] = [];
0 commit comments