Skip to content

Commit 288b1a8

Browse files
Merge pull request #15 from arko-betersport/master
Added attribute group information
2 parents 593a7fa + 648f915 commit 288b1a8

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/Model/Resolver/Products/DataPostProcessor/Attributes.php

+59-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use Magento\Swatches\Helper\Data;
2424
use ScandiPWA\Performance\Api\ProductsDataPostProcessorInterface;
2525
use ScandiPWA\Performance\Model\Resolver\ResolveInfoFieldsTrait;
26+
use Magento\Eav\Api\Data\AttributeGroupInterface;
27+
use Magento\Eav\Model\ResourceModel\Entity\Attribute\Group\CollectionFactory as GroupCollectionFactory;
2628

2729
/**
2830
* Class Attributes
@@ -54,6 +56,11 @@ class Attributes implements ProductsDataPostProcessorInterface
5456
*/
5557
protected $attributeRepository;
5658

59+
/**
60+
* @var GroupCollectionFactory
61+
*/
62+
protected $groupCollection;
63+
5764
/**
5865
* Attributes constructor.
5966
* @param Data $swatchHelper
@@ -65,12 +72,14 @@ public function __construct(
6572
Data $swatchHelper,
6673
CollectionFactory $productCollection,
6774
SearchCriteriaBuilder $searchCriteriaBuilder,
68-
ProductAttributeRepositoryInterface $attributeRepository
75+
ProductAttributeRepositoryInterface $attributeRepository,
76+
GroupCollectionFactory $groupCollection
6977
) {
7078
$this->swatchHelper = $swatchHelper;
7179
$this->productCollection = $productCollection;
7280
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
7381
$this->attributeRepository = $attributeRepository;
82+
$this->groupCollection = $groupCollection;
7483
}
7584

7685
/**
@@ -137,6 +146,49 @@ protected function appendWithValue(
137146
}
138147
}
139148

149+
/**
150+
* Append product attribute data with value, if value not found, strip the attribute from response
151+
* @param $attributes ProductAttributeInterface[]
152+
* @param $products array
153+
* @param $productAttributes
154+
* @throws LocalizedException
155+
*/
156+
protected function appendWithGroup(
157+
array $attributes,
158+
array $products,
159+
array &$productAttributes
160+
): void {
161+
162+
/** @var Product $product */
163+
$setIds = array_map(function ($product) {
164+
return $product->getAttributeSetId();
165+
}, $products);
166+
167+
// Retrieve all groups with used product Set Ids
168+
$groupCollection = $this->groupCollection->create()
169+
->addFieldToFilter('attribute_set_id', ['in' => $setIds])
170+
->load();
171+
172+
foreach ($products as $product) {
173+
$productId = $product->getId();
174+
$setId = $product->getAttributeSetId();
175+
176+
/** @var Attribute $attribute */
177+
foreach ($attributes as $attributeCode => $attribute) {
178+
179+
// Find the correct group for every attribute
180+
/** @var AttributeGroupInterface $group */
181+
foreach ($groupCollection as $group) {
182+
if ($attribute->isInGroup($setId, $group->getAttributeGroupId())) {
183+
$productAttributes[$productId][$attributeCode]['attribute_group_name'] = $group->getAttributeGroupName();
184+
$productAttributes[$productId][$attributeCode]['attribute_group_id'] = $group->getAttributeGroupId();
185+
$productAttributes[$productId][$attributeCode]['attribute_group_code'] = $group->getAttributeGroupCode();
186+
}
187+
}
188+
}
189+
}
190+
}
191+
140192
/**
141193
* Append options to attribute options
142194
*
@@ -381,6 +433,12 @@ public function process(
381433
$productAttributes
382434
);
383435

436+
$this->appendWithGroup(
437+
$attributes,
438+
$products,
439+
$productAttributes
440+
);
441+
384442
if ($isCollectOptions) {
385443
$this->appendWithOptions(
386444
$attributes,

src/etc/schema.graphqls

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ type AttributeWithValue {
1919
attribute_value: String
2020
attribute_label: String
2121
attribute_id: Int
22+
attribute_group_name: String
23+
attribute_group_code: String
24+
attribute_group_id: String
2225
attribute_options: [AttributeWithValueOption]
2326
}
2427

0 commit comments

Comments
 (0)