diff --git a/app/code/core/Mage/ConfigurableSwatches/Helper/Mediafallback.php b/app/code/core/Mage/ConfigurableSwatches/Helper/Mediafallback.php index acda04049dd..ca49d89b4c6 100644 --- a/app/code/core/Mage/ConfigurableSwatches/Helper/Mediafallback.php +++ b/app/code/core/Mage/ConfigurableSwatches/Helper/Mediafallback.php @@ -71,15 +71,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 = []; @@ -108,8 +108,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])) { @@ -124,9 +126,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 @@ -137,8 +139,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, ); } diff --git a/app/code/core/Mage/ConfigurableSwatches/Model/Resource/Catalog/Product/Attribute/Super/Collection.php b/app/code/core/Mage/ConfigurableSwatches/Model/Resource/Catalog/Product/Attribute/Super/Collection.php index f485132ad84..828e0fab583 100644 --- a/app/code/core/Mage/ConfigurableSwatches/Model/Resource/Catalog/Product/Attribute/Super/Collection.php +++ b/app/code/core/Mage/ConfigurableSwatches/Model/Resource/Catalog/Product/Attribute/Super/Collection.php @@ -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']; }