Skip to content

Commit d0d9cc5

Browse files
Merge branch '2.4-develop' into bluetooth_delivery
2 parents c348e0a + a77984d commit d0d9cc5

File tree

3 files changed

+53
-51
lines changed

3 files changed

+53
-51
lines changed

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

+10
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

+12-1
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

+31-50
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,67 +141,47 @@ 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;
149151
}
150152

153+
/** @var ChildCollection $childCollection */
154+
$childCollection = $this->childCollectionFactory->create();
151155
foreach ($this->parentProducts as $product) {
152-
$attributeData = $this->getAttributesCodes($product);
153-
/** @var ChildCollection $childCollection */
154-
$childCollection = $this->childCollectionFactory->create();
155156
$childCollection->setProductFilter($product);
156-
$childCollection->addWebsiteFilter($context->getExtensionAttributes()->getStore()->getWebsiteId());
157-
$this->collectionProcessor->process(
158-
$childCollection,
159-
$this->searchCriteriaBuilder->create(),
160-
$attributeData,
161-
$context
162-
);
163-
$childCollection->load();
164-
$this->collectionPostProcessor->process($childCollection, $attributeData);
165-
166-
/** @var Product $childProduct */
167-
foreach ($childCollection as $childProduct) {
168-
if ((int)$childProduct->getStatus() !== Status::STATUS_ENABLED) {
169-
continue;
170-
}
171-
$formattedChild = ['model' => $childProduct, 'sku' => $childProduct->getSku()];
172-
$parentId = (int)$childProduct->getParentId();
173-
if (!isset($this->childrenMap[$parentId])) {
174-
$this->childrenMap[$parentId] = [];
175-
}
176-
177-
$this->childrenMap[$parentId][] = $formattedChild;
178-
}
179157
}
180-
181-
return $this->childrenMap;
182-
}
183-
184-
/**
185-
* Get attributes codes for given product
186-
*
187-
* @param Product $currentProduct
188-
* @return array
189-
*/
190-
private function getAttributesCodes(Product $currentProduct): array
191-
{
192-
$attributeCodes = $this->attributeCodes;
193-
if ($currentProduct->getTypeId() == Configurable::TYPE_CODE) {
194-
$allowAttributes = $currentProduct->getTypeInstance()->getConfigurableAttributes($currentProduct);
195-
foreach ($allowAttributes as $attribute) {
196-
$productAttribute = $attribute->getProductAttribute();
197-
if (!\in_array($productAttribute->getAttributeCode(), $attributeCodes)) {
198-
$attributeCodes[] = $productAttribute->getAttributeCode();
199-
}
158+
$childCollection->addWebsiteFilter($context->getExtensionAttributes()->getStore()->getWebsiteId());
159+
160+
$attributeCodes = array_unique(array_merge($this->attributeCodes, $attributeCodes));
161+
162+
$this->collectionProcessor->process(
163+
$childCollection,
164+
$this->searchCriteriaBuilder->create(),
165+
$attributeCodes,
166+
$context
167+
);
168+
$this->collectionPostProcessor->process($childCollection, $attributeCodes);
169+
170+
/** @var Product $childProduct */
171+
foreach ($childCollection as $childProduct) {
172+
if ((int)$childProduct->getStatus() !== Status::STATUS_ENABLED) {
173+
continue;
200174
}
175+
$formattedChild = ['model' => $childProduct, 'sku' => $childProduct->getSku()];
176+
$parentId = (int)$childProduct->getParentId();
177+
if (!isset($this->childrenMap[$parentId])) {
178+
$this->childrenMap[$parentId] = [];
179+
}
180+
181+
$this->childrenMap[$parentId][] = $formattedChild;
201182
}
202183

203-
return $attributeCodes;
184+
return $this->childrenMap;
204185
}
205186

206187
/**

0 commit comments

Comments
 (0)