diff --git a/vendor/magento/module-configurable-product-graph-ql/Model/Variant/Collection.php b/vendor/magento/module-configurable-product-graph-ql/Model/Variant/Collection.php index f849139281393..2206abd279df4 100644 --- a/vendor/magento/module-configurable-product-graph-ql/Model/Variant/Collection.php +++ b/vendor/magento/module-configurable-product-graph-ql/Model/Variant/Collection.php @@ -7,9 +7,9 @@ namespace Magento\ConfigurableProductGraphQl\Model\Variant; +use Exception; use Magento\Catalog\Api\Data\ProductInterface; use Magento\Catalog\Model\Product; -use Magento\ConfigurableProduct\Model\Product\Type\Configurable; use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\Collection as ChildCollection; use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable\Product\CollectionFactory; use Magento\Framework\EntityManager\MetadataPool; @@ -91,6 +91,7 @@ public function __construct( * * @param Product $product * @return void + * @throws Exception */ public function addParentProduct(Product $product) : void { @@ -143,6 +144,7 @@ public function getChildProductsByParentId(int $id, ContextInterface $context, a * @param ContextInterface $context * @param array $attributeCodes * @return array + * @throws Exception */ private function fetch(ContextInterface $context, array $attributeCodes) : array { @@ -156,6 +158,11 @@ private function fetch(ContextInterface $context, array $attributeCodes) : array $childCollection->setProductFilter($product); } $childCollection->addWebsiteFilter($context->getExtensionAttributes()->getStore()->getWebsiteId()); + $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField(); + $childCollection->getSelect()->group('e.' . $linkField); + $childCollection->getSelect()->columns([ + 'parent_ids' => new \Zend_Db_Expr('GROUP_CONCAT(link_table.parent_id)') + ]); $attributeCodes = array_unique(array_merge($this->attributeCodes, $attributeCodes)); @@ -173,14 +180,14 @@ private function fetch(ContextInterface $context, array $attributeCodes) : array continue; } $formattedChild = ['model' => $childProduct, 'sku' => $childProduct->getSku()]; - $parentId = (int)$childProduct->getParentId(); - if (!isset($this->childrenMap[$parentId])) { - $this->childrenMap[$parentId] = []; + $parentIds = $childProduct->getParentIds() ? explode(',', $childProduct->getParentIds()) : []; + foreach ($parentIds as $parentId) { + if (!isset($this->childrenMap[$parentId])) { + $this->childrenMap[$parentId] = []; + } + $this->childrenMap[$parentId][] = $formattedChild; } - - $this->childrenMap[$parentId][] = $formattedChild; } - return $this->childrenMap; }