Skip to content

Commit e22d32d

Browse files
author
vitaliyboyko
committed
graphQl-256: fetching variation attributes on configurable product
2 parents 349ad46 + 9bee85b commit e22d32d

File tree

3 files changed

+47
-16
lines changed

3 files changed

+47
-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->addParentProduct($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: 44 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 $parentProducts = [];
5353

5454
/**
5555
* @var array
@@ -83,19 +83,22 @@ 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 addParentProduct(Product $product) : void
9292
{
93-
if (!in_array($id, $this->parentIds) && !empty($this->childrenMap)) {
93+
if (isset($this->parentProducts[$product->getId()])) {
94+
return;
95+
}
96+
97+
if (!empty($this->childrenMap)) {
9498
$this->childrenMap = [];
95-
$this->parentIds[] = $id;
96-
} elseif (!in_array($id, $this->parentIds)) {
97-
$this->parentIds[] = $id;
99+
98100
}
101+
$this->parentProducts[$product->getId()] = $product;
99102
}
100103

101104
/**
@@ -130,20 +133,24 @@ public function getChildProductsByParentId(int $id) : array
130133
* Fetch all children products from parent id's.
131134
*
132135
* @return array
136+
* @throws \Exception
133137
*/
134138
private function fetch() : array
135139
{
136-
if (empty($this->parentIds) || !empty($this->childrenMap)) {
140+
if (empty($this->parentProducts) || !empty($this->childrenMap)) {
137141
return $this->childrenMap;
138142
}
139143

140144
$linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
141-
foreach ($this->parentIds as $id) {
145+
foreach ($this->parentProducts as $product) {
146+
147+
$attributeData = $this->getAttributesCodes($product);
142148
/** @var ChildCollection $childCollection */
143149
$childCollection = $this->childCollectionFactory->create();
150+
$childCollection->addAttributeToSelect($attributeData);
151+
144152
/** @var Product $product */
145-
$product = $this->productFactory->create();
146-
$product->setData($linkField, $id);
153+
$product->setData($linkField, $product->getId());
147154
$childCollection->setProductFilter($product);
148155

149156
/** @var Product $childProduct */
@@ -160,4 +167,28 @@ private function fetch() : array
160167

161168
return $this->childrenMap;
162169
}
170+
171+
/**
172+
* Get attributes codes
173+
*
174+
* @param Product $currentProduct
175+
* @return array
176+
*/
177+
private function getAttributesCodes(Product $currentProduct): array
178+
{
179+
$attributeCodes = [];
180+
$attributes = $currentProduct->getAttributes();
181+
foreach ($attributes as $key => $attribute) {
182+
$isVisible = (int)$attribute->getIsVisibleOnFront();
183+
if (!$isVisible) {
184+
continue;
185+
}
186+
if (!in_array($key, $attributeCodes)) {
187+
continue;
188+
}
189+
$attributeCodes[] = $key;
190+
}
191+
192+
return $attributeCodes;
193+
}
163194
}

0 commit comments

Comments
 (0)