Skip to content

Commit 8fb1699

Browse files
committed
ACP2E-3190: [Cloud] Products graphql having error when same simple product has assigned to multiple configurable products
- Initial Commit
1 parent 4d193eb commit 8fb1699

File tree

3 files changed

+25
-97
lines changed

3 files changed

+25
-97
lines changed

app/code/Magento/ConfigurableProductGraphQl/Model/ResourceModel/Product/Type/GetChildrenIdsByParentId.php

Lines changed: 0 additions & 65 deletions
This file was deleted.

app/code/Magento/ConfigurableProductGraphQl/Model/Variant/Collection.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77

88
namespace Magento\ConfigurableProductGraphQl\Model\Variant;
99

10+
use Exception;
1011
use Magento\Catalog\Api\Data\ProductInterface;
1112
use Magento\Catalog\Model\Product;
12-
use Magento\ConfigurableProductGraphQl\Model\ResourceModel\Product\Type\GetChildrenIdsByParentId;
1313
use Magento\Catalog\Model\ResourceModel\Product\Collection as ChildCollection;
1414
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
15-
use Magento\Framework\App\ObjectManager;
1615
use Magento\Framework\EntityManager\MetadataPool;
1716
use Magento\Framework\Api\SearchCriteriaBuilder;
18-
use Magento\Framework\Exception\LocalizedException;
1917
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
2018
use Magento\GraphQl\Model\Query\ContextInterface;
2119
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface;
@@ -24,7 +22,6 @@
2422

2523
/**
2624
* Collection for fetching configurable child product data.
27-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2825
*/
2926
class Collection implements ResetAfterRequestInterface
3027
{
@@ -68,42 +65,33 @@ class Collection implements ResetAfterRequestInterface
6865
*/
6966
private $collectionPostProcessor;
7067

71-
/**
72-
* @var GetChildrenIdsByParentId
73-
*/
74-
private $getChildrenIdsByParentId;
75-
7668
/**
7769
* @param CollectionFactory $childCollectionFactory
7870
* @param SearchCriteriaBuilder $searchCriteriaBuilder
7971
* @param MetadataPool $metadataPool
8072
* @param CollectionProcessorInterface $collectionProcessor
8173
* @param CollectionPostProcessor $collectionPostProcessor
82-
* @param GetChildrenIdsByParentId|null $getChildrenIdsByParentId
8374
*/
8475
public function __construct(
8576
CollectionFactory $childCollectionFactory,
8677
SearchCriteriaBuilder $searchCriteriaBuilder,
8778
MetadataPool $metadataPool,
8879
CollectionProcessorInterface $collectionProcessor,
89-
CollectionPostProcessor $collectionPostProcessor,
90-
?GetChildrenIdsByParentId $getChildrenIdsByParentId = null
80+
CollectionPostProcessor $collectionPostProcessor
9181
) {
9282
$this->childCollectionFactory = $childCollectionFactory;
9383
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
9484
$this->metadataPool = $metadataPool;
9585
$this->collectionProcessor = $collectionProcessor;
9686
$this->collectionPostProcessor = $collectionPostProcessor;
97-
$this->getChildrenIdsByParentId = $getChildrenIdsByParentId
98-
?: ObjectManager::getInstance()->get(GetChildrenIdsByParentId::class);
9987
}
10088

10189
/**
10290
* Add parent to collection filter
10391
*
10492
* @param Product $product
10593
* @return void
106-
* @throws \Exception
94+
* @throws Exception
10795
*/
10896
public function addParentProduct(Product $product) : void
10997
{
@@ -138,7 +126,7 @@ public function addEavAttributes(array $attributeCodes) : void
138126
* @param ContextInterface $context
139127
* @param array $attributeCodes
140128
* @return array
141-
* @throws LocalizedException
129+
* @throws Exception
142130
*/
143131
public function getChildProductsByParentId(int $id, ContextInterface $context, array $attributeCodes) : array
144132
{
@@ -157,7 +145,7 @@ public function getChildProductsByParentId(int $id, ContextInterface $context, a
157145
* @param ContextInterface $context
158146
* @param array $attributeCodes
159147
* @return array
160-
* @throws LocalizedException
148+
* @throws Exception
161149
*/
162150
private function fetch(ContextInterface $context, array $attributeCodes) : array
163151
{
@@ -167,9 +155,18 @@ private function fetch(ContextInterface $context, array $attributeCodes) : array
167155

168156
/** @var ChildCollection $childCollection */
169157
$childCollection = $this->childCollectionFactory->create();
170-
$childrenIdsByParent = $this->getChildrenIdsByParentId->execute(array_keys($this->parentProducts));
158+
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
171159
$childCollection->addWebsiteFilter($context->getExtensionAttributes()->getStore()->getWebsiteId());
172-
$childCollection->addIdFilter(array_keys($childrenIdsByParent));
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);
173170
$attributeCodes = array_unique(array_merge($this->attributeCodes, $attributeCodes));
174171

175172
$this->collectionProcessor->process(
@@ -186,7 +183,9 @@ private function fetch(ContextInterface $context, array $attributeCodes) : array
186183
continue;
187184
}
188185
$formattedChild = ['model' => $childProduct, 'sku' => $childProduct->getSku()];
189-
foreach ($childrenIdsByParent[$childProduct->getId()] as $parentId) {
186+
187+
$parentIds = preg_split ("/\,/", (string) $childProduct->getParentId());
188+
foreach ($parentIds as $parentId) {
190189
if (!isset($this->childrenMap[$parentId])) {
191190
$this->childrenMap[$parentId] = [];
192191
}

dev/tests/api-functional/testsuite/Magento/GraphQl/ConfigurableProduct/MultipleConfigurableProductWithSameSimpleProductTest.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
<?php
2-
/************************************************************************
3-
*
2+
/**
43
* Copyright 2024 Adobe
54
* All Rights Reserved.
6-
*
7-
* NOTICE: All information contained herein is, and remains
8-
* the property of Adobe and its suppliers, if any. The intellectual
9-
* and technical concepts contained herein are proprietary to Adobe
10-
* and its suppliers and are protected by all applicable intellectual
11-
* property laws, including trade secret and copyright laws.
12-
* Dissemination of this information or reproduction of this material
13-
* is strictly forbidden unless prior written permission is obtained
14-
* from Adobe.
15-
* ************************************************************************
165
*/
176
declare(strict_types=1);
187

@@ -81,6 +70,11 @@ public function testMultipleConfigurableProductCanHaveSameSimpleProduct()
8170

8271
self::assertArrayNotHasKey('errors', $result);
8372
self::assertNotEmpty($result['products']);
73+
self::assertCount(2, $result['products']['items']);
74+
self::assertNotEmpty($result['products']['items'][0]['variants'][0]);
75+
self::assertCount(2, $result['products']['items'][0]['variants'][0]);
76+
self::assertNotEmpty($result['products']['items'][1]['variants'][0]);
77+
self::assertCount(2, $result['products']['items'][1]['variants'][0]);
8478
}
8579

8680
/**

0 commit comments

Comments
 (0)