Skip to content

Commit b6169a6

Browse files
committed
LYNX-227: Eliminated double attributes loading
1 parent 23a7519 commit b6169a6

File tree

3 files changed

+29
-35
lines changed

3 files changed

+29
-35
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,16 @@ public function getAttributesByProductId(int $productId): array
105105
return $attributes[$productId];
106106
}
107107

108+
/**
109+
* Retrieve all attributes
110+
*
111+
* @return array
112+
*/
113+
public function getAttributes(): array
114+
{
115+
return $this->fetch();
116+
}
117+
108118
/**
109119
* Fetch attribute data
110120
*

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,18 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
9898
$this->optionCollection->addProductId((int)$value[$linkField]);
9999

100100
$result = function () use ($value, $linkField, $context) {
101-
$children = $this->variantCollection->getChildProductsByParentId((int)$value[$linkField], $context);
101+
$attributeCodes = [];
102+
foreach ($this->optionCollection->getAttributes() as $productAttributes) {
103+
foreach ($productAttributes as $attribute) {
104+
$attributeCodes[] = $attribute['attribute_code'];
105+
}
106+
}
107+
$attributeCodes = array_unique($attributeCodes);
108+
$children = $this->variantCollection->getChildProductsByParentId(
109+
(int)$value[$linkField],
110+
$context,
111+
$attributeCodes
112+
);
102113
$options = $this->optionCollection->getAttributesByProductId((int)$value[$linkField]);
103114
$variants = [];
104115
/** @var Product $child */

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

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,12 @@ public function addEavAttributes(array $attributeCodes) : void
123123
*
124124
* @param int $id
125125
* @param ContextInterface $context
126+
* @param array $attributeCodes
126127
* @return array
127128
*/
128-
public function getChildProductsByParentId(int $id, ContextInterface $context) : array
129+
public function getChildProductsByParentId(int $id, ContextInterface $context, array $attributeCodes) : array
129130
{
130-
$childrenMap = $this->fetch($context);
131+
$childrenMap = $this->fetch($context, $attributeCodes);
131132

132133
if (!isset($childrenMap[$id])) {
133134
return [];
@@ -140,9 +141,10 @@ public function getChildProductsByParentId(int $id, ContextInterface $context) :
140141
* Fetch all children products from parent id's.
141142
*
142143
* @param ContextInterface $context
144+
* @param array $attributeCodes
143145
* @return array
144146
*/
145-
private function fetch(ContextInterface $context) : array
147+
private function fetch(ContextInterface $context, array $attributeCodes) : array
146148
{
147149
if (empty($this->parentProducts) || !empty($this->childrenMap)) {
148150
return $this->childrenMap;
@@ -152,20 +154,13 @@ private function fetch(ContextInterface $context) : array
152154
$childCollection = $this->childCollectionFactory->create();
153155
$childCollection->addWebsiteFilter($context->getExtensionAttributes()->getStore()->getWebsiteId());
154156

155-
$attributeCodes = [];
156-
foreach ($this->parentProducts as $product) {
157-
$childCollection->setProductFilter($product);
158-
$attributeCodes[] = $this->getAttributesCodes($product);
159-
}
160-
$attributeData = array_unique(array_merge([], ...$attributeCodes));
161-
162157
$this->collectionProcessor->process(
163158
$childCollection,
164159
$this->searchCriteriaBuilder->create(),
165-
$attributeData,
160+
$attributeCodes,
166161
$context
167162
);
168-
$this->collectionPostProcessor->process($childCollection, $attributeData);
163+
$this->collectionPostProcessor->process($childCollection, $attributeCodes);
169164

170165
/** @var Product $childProduct */
171166
foreach ($childCollection as $childProduct) {
@@ -184,28 +179,6 @@ private function fetch(ContextInterface $context) : array
184179
return $this->childrenMap;
185180
}
186181

187-
/**
188-
* Get attributes codes for given product
189-
*
190-
* @param Product $currentProduct
191-
* @return array
192-
*/
193-
private function getAttributesCodes(Product $currentProduct): array
194-
{
195-
$attributeCodes = $this->attributeCodes;
196-
if ($currentProduct->getTypeId() == Configurable::TYPE_CODE) {
197-
$allowAttributes = $currentProduct->getTypeInstance()->getConfigurableAttributes($currentProduct);
198-
foreach ($allowAttributes as $attribute) {
199-
$productAttribute = $attribute->getProductAttribute();
200-
if (!\in_array($productAttribute->getAttributeCode(), $attributeCodes)) {
201-
$attributeCodes[] = $productAttribute->getAttributeCode();
202-
}
203-
}
204-
}
205-
206-
return $attributeCodes;
207-
}
208-
209182
/**
210183
* @inheritDoc
211184
*/

0 commit comments

Comments
 (0)