23
23
use Magento \Swatches \Helper \Data ;
24
24
use ScandiPWA \Performance \Api \ProductsDataPostProcessorInterface ;
25
25
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 ;
26
28
27
29
/**
28
30
* Class Attributes
@@ -54,6 +56,11 @@ class Attributes implements ProductsDataPostProcessorInterface
54
56
*/
55
57
protected $ attributeRepository ;
56
58
59
+ /**
60
+ * @var GroupCollectionFactory
61
+ */
62
+ protected $ groupCollection ;
63
+
57
64
/**
58
65
* Attributes constructor.
59
66
* @param Data $swatchHelper
@@ -65,12 +72,14 @@ public function __construct(
65
72
Data $ swatchHelper ,
66
73
CollectionFactory $ productCollection ,
67
74
SearchCriteriaBuilder $ searchCriteriaBuilder ,
68
- ProductAttributeRepositoryInterface $ attributeRepository
75
+ ProductAttributeRepositoryInterface $ attributeRepository ,
76
+ GroupCollectionFactory $ groupCollection
69
77
) {
70
78
$ this ->swatchHelper = $ swatchHelper ;
71
79
$ this ->productCollection = $ productCollection ;
72
80
$ this ->searchCriteriaBuilder = $ searchCriteriaBuilder ;
73
81
$ this ->attributeRepository = $ attributeRepository ;
82
+ $ this ->groupCollection = $ groupCollection ;
74
83
}
75
84
76
85
/**
@@ -137,6 +146,49 @@ protected function appendWithValue(
137
146
}
138
147
}
139
148
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
+
140
192
/**
141
193
* Append options to attribute options
142
194
*
@@ -381,6 +433,12 @@ public function process(
381
433
$ productAttributes
382
434
);
383
435
436
+ $ this ->appendWithGroup (
437
+ $ attributes ,
438
+ $ products ,
439
+ $ productAttributes
440
+ );
441
+
384
442
if ($ isCollectOptions ) {
385
443
$ this ->appendWithOptions (
386
444
$ attributes ,
0 commit comments