Magento – Magento 1.9 configurable swatches ordered by position

ce-1.9.1.0swatches

When I use the new configurable swatches with Magento 1.9.1, the configurable items are not sorted like in the drop-down.

For example sizes, I want sizes sorted like S, M, L, XL. That is also how I sorted them by position in the attribute settings.

Now with the configurable swatches they seem sorted at random, like M, XL, S, L. It's not alphabetical or in position, I can't figure out what is going wrong.

After setting up the use of these swatches I re-indexed and cleared cache.

Best Answer

I had the same issue and applied the 2nd fix given here: https://stackoverflow.com/questions/27270918/magento-1-9-1-not-sorting-configurable-product-attributes-dropdown-by-position:

[...] copy app/code/core/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php to local folder app/code/local/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php and apply this patch:

Index: app/code/local/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php
===================================================================
--- app/code/local/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php
+++ app/code/local/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php
@@ -301,7 +301,28 @@
                     }
                 }
             }

+            /**
+             * Mage 1.9+ fix for configurable attribute options not sorting to position
+             * @author Harshit <support@cubixws.co.uk>
+             */
+            $sortOrder = 1;
+            foreach ($this->_items as $item) {
+                $productAttribute = $item->getProductAttribute();
+                if (!($productAttribute instanceof Mage_Eav_Model_Entity_Attribute_Abstract)) {
+                    continue;
+                }
+                $options = $productAttribute->getFrontend()->getSelectOptions();
+                foreach ($options as $option) {
+                    if (!$option['value']) continue;
+                    if (isset($values[$item->getId() . ':' . $option['value']])) {
+                        $values[$item->getId() . ':' . $option['value']]['order'] = $sortOrder++;
+                    }
+                }
+            }
+            usort($values, function($a, $b) {
+                return $a['order'] - $b['order'];
+            });
+            
             foreach ($values as $data) {
                 $this->getItemById($data['product_super_attribute_id'])->addPrice($data);
             }

If you are hesitant of copying across a core file to local folder then I can create a quick module, <rewrite> this Collection.php file and just override the _loadPrices() function and introduce this fix.

You just have to copy the core file to app/code/core/Mage/Catalog/Model/Resource/Product/Type/Configurable/Attribute/Collection.php and add the given code - it was line 304 for me.

It worked fine for my size attribute swatches.

Related Topic