Skip to content

Commit 802ab7a

Browse files
Merge pull request #6 from RoboLV/patch-1
Remove unused attribute optins values
2 parents e705ec8 + 6384ce2 commit 802ab7a

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

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

+47-4
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ protected function appendWithValue(
128128

129129
if (!$attributeValue) {
130130
// Remove all empty attributes
131-
// unset($productAttributes[$productId][$attributeCode]);
132131
continue;
133132
}
134133

@@ -140,6 +139,7 @@ protected function appendWithValue(
140139

141140
/**
142141
* Append options to attribute options
142+
*
143143
* @param $attributes ProductAttributeInterface[]
144144
* @param $products ExtensibleDataInterface[]
145145
* @param $productAttributes array
@@ -150,7 +150,7 @@ protected function appendWithOptions(
150150
array $products,
151151
array $swatchAttributes,
152152
array &$productAttributes
153-
) {
153+
): void {
154154
$attributeCodes = array_keys($attributes);
155155
$searchCriteria = $this->searchCriteriaBuilder
156156
->addFilter('main_table.attribute_code', $attributeCodes, 'in')
@@ -168,17 +168,60 @@ protected function appendWithOptions(
168168
foreach ($products as $product) {
169169
$id = $product->getId();
170170

171+
$configuration = $product->getTypeId() === 'configurable'
172+
? $product->getTypeInstance()->getConfigurableOptions($product)
173+
: [];
174+
171175
foreach ($detailedAttributes as $attribute) {
172176
$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+
173213
$options = $attribute->getOptions();
174214
array_shift($options);
175-
176215
$productAttributes[$id][$key]['attribute_options'] = [];
177216

178217
foreach ($options as $option) {
179218
$value = $option->getValue();
180-
$optionIds[] = $value;
181219

220+
if (!isset($values[$value])) {
221+
continue;
222+
}
223+
224+
$optionIds[] = $value;
182225
$productAttributes[$id][$key]['attribute_options'][$value] = [
183226
'value' => $value,
184227
'label' => $option->getLabel()

0 commit comments

Comments
 (0)