Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento 1.9.1 not sorting Configurable product attributes dropdown by position

Fresh install of Magento 1.9.1.

Magento is ignoring the attribute position set in Catalogue->Attributes->Manage Attributes->Manage Labels/Options for a configurable product drop down. Instead it is using the Product ID to determine list order.

Have compared the following files/functions and, apart from a small tax calculation, none of the code has changed since 1.7.0.2.

Mage/Catalog/Model/Product/Type/Configuarable.php:

public function getConfigurableAttributes($product = null)

Mage/Catalog/Model/Product/Option.php:

public function getProductOptionCollection(Mage_Catalog_Model_Product $product)

Mage/Catalog/Block/Product/View/Type/Configuarable.php:

public function getJsonConfig()

I have also tested on a copy database of a live site and all the attribute sorting is based on Product ID.

To replicate:

  1. Create an attribute - Colour
  2. Add labels - Black, Red, Green, Blue
  3. Save attribute.
  4. Create configurable and simple associated products with the attributes in the above order.

Edit attribute and change label positions. Blue 0, Green 1, Red 3, Black 4

When viewing product Magento still sorts the attributes by Product ID and ignores positions.

like image 552
hejhog Avatar asked Dec 03 '14 11:12

hejhog


1 Answers

The answer by Meogi works but is not the perfect answer as it will only sort the options on the frontend. Try creating an order from the admin panel for a configurable product. You will still get the incorrectly sorted attribute option list.

Instead, you can 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 <[email protected]>
+             */
+            $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.

like image 179
Harshit Avatar answered Sep 20 '22 22:09

Harshit