@@ -128,7 +128,6 @@ protected function appendWithValue(
128
128
129
129
if (!$ attributeValue ) {
130
130
// Remove all empty attributes
131
- // unset($productAttributes[$productId][$attributeCode]);
132
131
continue ;
133
132
}
134
133
@@ -140,6 +139,7 @@ protected function appendWithValue(
140
139
141
140
/**
142
141
* Append options to attribute options
142
+ *
143
143
* @param $attributes ProductAttributeInterface[]
144
144
* @param $products ExtensibleDataInterface[]
145
145
* @param $productAttributes array
@@ -150,7 +150,7 @@ protected function appendWithOptions(
150
150
array $ products ,
151
151
array $ swatchAttributes ,
152
152
array &$ productAttributes
153
- ) {
153
+ ): void {
154
154
$ attributeCodes = array_keys ($ attributes );
155
155
$ searchCriteria = $ this ->searchCriteriaBuilder
156
156
->addFilter ('main_table.attribute_code ' , $ attributeCodes , 'in ' )
@@ -168,17 +168,60 @@ protected function appendWithOptions(
168
168
foreach ($ products as $ product ) {
169
169
$ id = $ product ->getId ();
170
170
171
+ $ configuration = $ product ->getTypeId () === 'configurable '
172
+ ? $ product ->getTypeInstance ()->getConfigurableOptions ($ product )
173
+ : [];
174
+
171
175
foreach ($ detailedAttributes as $ attribute ) {
172
176
$ key = $ attribute ->getAttributeCode ();
177
+
178
+ if (!isset ($ productAttributes [$ id ][$ key ])) {
179
+ continue ;
180
+ }
181
+
182
+ $ productAttributes [$ id ][$ key ]['attribute_options ' ] = [];
183
+ $ variantAttributeValues = [];
184
+
185
+ if ($ product ->getTypeId () === 'configurable ' ) {
186
+ $ attributeId = $ attribute ->getAttributeId ();
187
+ $ productAttributeVariants = $ configuration [$ attributeId ] ?? [];
188
+
189
+ $ variantAttributeValues = array_filter (
190
+ array_column ($ productAttributeVariants , 'value_index ' )
191
+ );
192
+ }
193
+
194
+ if (
195
+ !isset ($ productAttributes [$ id ][$ key ]['attribute_value ' ])
196
+ && !count ($ variantAttributeValues )
197
+ ) {
198
+ continue ;
199
+ }
200
+
201
+ // Merge all attribute values into one array(map) and flip values with keys (convert to hash map)
202
+ // This used to bring faster access check for value existence
203
+ // Hash key variable check is faster then traditional search
204
+ $ values = array_flip ( // Flip Array
205
+ array_merge ( // phpcs:ignore
206
+ array_filter ( // explode might return array with empty value, remove such values
207
+ explode (', ' , $ productAttributes [$ id ][$ key ]['attribute_value ' ] ?? '' )
208
+ ),
209
+ $ variantAttributeValues
210
+ )
211
+ );
212
+
173
213
$ options = $ attribute ->getOptions ();
174
214
array_shift ($ options );
175
-
176
215
$ productAttributes [$ id ][$ key ]['attribute_options ' ] = [];
177
216
178
217
foreach ($ options as $ option ) {
179
218
$ value = $ option ->getValue ();
180
- $ optionIds [] = $ value ;
181
219
220
+ if (!isset ($ values [$ value ])) {
221
+ continue ;
222
+ }
223
+
224
+ $ optionIds [] = $ value ;
182
225
$ productAttributes [$ id ][$ key ]['attribute_options ' ][$ value ] = [
183
226
'value ' => $ value ,
184
227
'label ' => $ option ->getLabel ()
0 commit comments