Skip to content

Commit 9bee85b

Browse files
Fixed variant collection
1 parent 7755eef commit 9bee85b

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

app/code/Magento/ConfigurableProductGraphQl/Model/Resolver/ConfigurableVariant.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
8585
return $this->valueFactory->create($result);
8686
}
8787

88-
$this->variantCollection->addParentId((int)$value[$linkField]);
88+
$this->variantCollection->addParentId($value['model']);
8989
$fields = $this->getProductFields($info);
9090
$matchedFields = $this->attributeCollection->getRequestAttributes($fields);
9191
$this->variantCollection->addEavAttributes($matchedFields);

app/code/Magento/ConfigurableProductGraphQl/Model/Resolver/Variant/Attributes.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ public function resolve(
3535
$data = [];
3636
foreach ($value['options'] as $option) {
3737
$code = $option['attribute_code'];
38-
if (!isset($value['product'][$code])) {
38+
if (!isset($value['product']['model'][$code])) {
3939
continue;
4040
}
4141

4242
foreach ($option['values'] as $optionValue) {
43-
if ($optionValue['value_index'] != $value['product'][$code]) {
43+
if ($optionValue['value_index'] != $value['product']['model'][$code]) {
4444
continue;
4545
}
4646
$data[] = [

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

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ class Collection
4747
private $metadataPool;
4848

4949
/**
50-
* @var int[]
50+
* @var Product[]
5151
*/
52-
private $parentIds = [];
52+
private $parentProduct = [];
5353

5454
/**
5555
* @var array
@@ -83,18 +83,18 @@ public function __construct(
8383
}
8484

8585
/**
86-
* Add parent Id to collection filter
86+
* Add parent to collection filter
8787
*
88-
* @param int $id
88+
* @param Product $product
8989
* @return void
9090
*/
91-
public function addParentId(int $id) : void
91+
public function addParentId(Product $product) : void
9292
{
93-
if (!in_array($id, $this->parentIds) && !empty($this->childrenMap)) {
93+
if (!in_array($product, $this->parentProduct) && !empty($this->childrenMap)) {
9494
$this->childrenMap = [];
95-
$this->parentIds[] = $id;
96-
} elseif (!in_array($id, $this->parentIds)) {
97-
$this->parentIds[] = $id;
95+
$this->parentProduct[] = $product;
96+
} elseif (!in_array($product, $this->parentProduct)) {
97+
$this->parentProduct[] = $product;
9898
}
9999
}
100100

@@ -130,20 +130,24 @@ public function getChildProductsByParentId(int $id) : array
130130
* Fetch all children products from parent id's.
131131
*
132132
* @return array
133+
* @throws \Exception
133134
*/
134135
private function fetch() : array
135136
{
136-
if (empty($this->parentIds) || !empty($this->childrenMap)) {
137+
if (empty($this->parentProduct) || !empty($this->childrenMap)) {
137138
return $this->childrenMap;
138139
}
139140

140141
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
141-
foreach ($this->parentIds as $id) {
142+
foreach ($this->parentProduct as $product) {
143+
144+
$attributeData = $this->getAttributesCode($product);
142145
/** @var ChildCollection $childCollection */
143146
$childCollection = $this->childCollectionFactory->create();
147+
$childCollection->addAttributeToSelect($attributeData);
148+
144149
/** @var Product $product */
145-
$product = $this->productFactory->create();
146-
$product->setData($linkField, $id);
150+
$product->setData($linkField, $product->getId());
147151
$childCollection->setProductFilter($product);
148152

149153
/** @var Product $childProduct */
@@ -160,4 +164,24 @@ private function fetch() : array
160164

161165
return $this->childrenMap;
162166
}
167+
168+
/**
169+
* Get attributes code
170+
*
171+
* @param \Magento\Catalog\Model\Product $currentProduct
172+
* @return array
173+
*/
174+
private function getAttributesCode(Product $currentProduct): array
175+
{
176+
$attributeCode = [];
177+
$allowAttributes = $currentProduct->getTypeInstance()->getConfigurableAttributes($currentProduct);
178+
foreach ($allowAttributes as $attribute) {
179+
$productAttribute = $attribute->getProductAttribute();
180+
if (!\in_array($productAttribute->getAttributeCode(), $attributeCode)) {
181+
$attributeCode[] = $productAttribute->getAttributeCode();
182+
}
183+
}
184+
185+
return $attributeCode;
186+
}
163187
}

0 commit comments

Comments
 (0)