Skip to content

Commit a060fa7

Browse files
Copilotaddison74
andcommitted
Simplify sorting logic and add clarifying comments
Co-authored-by: addison74 <8360474+addison74@users.noreply.github.com>
1 parent 71d51ac commit a060fa7

File tree

2 files changed

+6
-12
lines changed

2 files changed

+6
-12
lines changed

app/code/core/Mage/ConfigurableSwatches/Helper/Mediafallback.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ public function attachProductChildrenAttributeMapping(array $parentProducts, $st
138138
}
139139

140140
if ($listSwatchValues !== []) {
141+
// Preserve the sort order from $optionLabelsOriginal while maintaining the option IDs as keys.
142+
// array_intersect_key returns entries from the first array that have matching keys in the second,
143+
// which ensures we get the original (non-normalized) labels in the correct sort order.
141144
$listSwatchValues = array_replace(
142145
array_intersect_key($optionLabelsOriginal, $listSwatchValues),
143146
$listSwatchValues,

app/code/core/Mage/ConfigurableSwatches/Model/Resource/Catalog/Product/Attribute/Super/Collection.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,21 +124,12 @@ protected function _getOptionLabels()
124124

125125
$resultSet = $this->getConnection()->query($select);
126126
$labels = [];
127-
$sortOrder = [];
128127
while ($option = $resultSet->fetch()) {
129-
$optionId = $option['option_id'];
130-
$labels[$optionId][$option['store_id']] = $option['label'];
131-
// Track the order in which we see each option_id (already sorted by sort_order)
132-
if (!isset($sortOrder[$optionId])) {
133-
$sortOrder[$optionId] = count($sortOrder);
134-
}
128+
// PHP arrays maintain insertion order, so as we iterate through the sorted query results,
129+
// the option IDs will be added in the correct sort_order
130+
$labels[$option['option_id']][$option['store_id']] = $option['label'];
135131
}
136132

137-
// Sort the labels array by the order we encountered the option_ids
138-
uksort($labels, function ($a, $b) use ($sortOrder) {
139-
return ($sortOrder[$a] ?? PHP_INT_MAX) <=> ($sortOrder[$b] ?? PHP_INT_MAX);
140-
});
141-
142133
return $labels;
143134
}
144135

0 commit comments

Comments
 (0)