Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions app/code/core/Mage/ConfigurableSwatches/Helper/Mediafallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ public function attachProductChildrenAttributeMapping(array $parentProducts, $st
->setStoreId($storeId)
;

$optionLabels = [];
$optionLabelsOriginal = [];
foreach ($configAttributes as $attribute) {
$optionLabels += $attribute->getOptionLabels();
$optionLabelsOriginal += $attribute->getOptionLabels();
}

// normalize to all lower case before we start using them
// normalize to all lower case before we start using them for image matching
$optionLabels = array_map(function ($value) {
return array_map(Mage_ConfigurableSwatches_Helper_Data::normalizeKey(...), $value);
}, $optionLabels);
}, $optionLabelsOriginal);

foreach ($parentProducts as $parentProduct) {
$mapping = [];
Expand Down Expand Up @@ -107,8 +107,10 @@ public function attachProductChildrenAttributeMapping(array $parentProducts, $st
continue;
}

// using default value as key unless store-specific label is present
// using default value as key unless store-specific label is present (normalized for image matching)
$optionLabel = $optionLabels[$optionId][$storeId] ?? $optionLabels[$optionId][0];
// Get the original (non-normalized) label for display
$optionLabelOriginal = $optionLabelsOriginal[$optionId][$storeId] ?? $optionLabelsOriginal[$optionId][0];

// initialize arrays if not present
if (!isset($mapping[$optionLabel])) {
Expand All @@ -123,9 +125,9 @@ public function attachProductChildrenAttributeMapping(array $parentProducts, $st
$mapping[$optionLabel]['labels'] = $optionLabels[$optionId];

if ($attribute->getAttributeId() == $listSwatchAttr->getAttributeId()
&& !in_array($mapping[$optionLabel]['label'], $listSwatchValues)
&& !in_array($optionLabelOriginal, $listSwatchValues)
) {
$listSwatchValues[$optionId] = $mapping[$optionLabel]['label'];
$listSwatchValues[$optionId] = $optionLabelOriginal;
$listSwatchStockValues[$optionId] = $isInStock;
}
} // end looping child products
Expand All @@ -136,8 +138,11 @@ public function attachProductChildrenAttributeMapping(array $parentProducts, $st
}

if ($listSwatchValues !== []) {
// Preserve the sort order from $optionLabelsOriginal while maintaining the option IDs as keys.
// array_intersect_key returns entries from the first array that have matching keys in the second,
// which ensures we get the original (non-normalized) labels in the correct sort order.
$listSwatchValues = array_replace(
array_intersect_key($optionLabels, $listSwatchValues),
array_intersect_key($optionLabelsOriginal, $listSwatchValues),
$listSwatchValues,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,14 @@ protected function _getOptionLabels()
->where(
'labels.store_id IN (?)',
[Mage_Catalog_Model_Abstract::DEFAULT_STORE_ID, $this->getStoreId()],
);
)
->order('options.sort_order ASC');

$resultSet = $this->getConnection()->query($select);
$labels = [];
while ($option = $resultSet->fetch()) {
// PHP arrays maintain insertion order, so as we iterate through the sorted query results,
// the option IDs will be added in the correct sort_order
$labels[$option['option_id']][$option['store_id']] = $option['label'];
}

Expand Down
Loading