magento programatically update sort_order on option dropdown labels

magento programatically update sort_order on option dropdown labels

September 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
magento programatically update sort_order on option dropdown labels

Programmatically Update Sort Order of Option Dropdown Labels in Magento

When working with Magento’s attribute options, one common task is updating the sort order without modifying the option labels themselves. This can be particularly useful when you want to sort options based on certain criteria without changing their display name.

Let me guide you through a step-by-step process to update the sort order of option dropdown labels in Magento programmatically.

Retrieving Existing Options and Sort Order

First, we need to retrieve all the existing options for an attribute. This can be done using the Mage_Core_Model_Resource_Eav_Attribute model.

$attribute = Mage::getModel('catalog/resource_eav_attribute')->loadByCode('catalog_product', 'manufacturer');
$optionsCollection = $attribute->getSource()->getAllOptions(false);

In this example, we’re loading the manufacturer attribute from the catalog_product entity. The $optionsCollection will contain an array of all options with their current labels and sort orders.

Sorting Options Based on Custom Criteria

Now let’s say you want to sort these manufacturer options based on the total product view count for each manufacturer. To do this, you’ll need to write a custom function that retrieves the view counts and sorts the options accordingly.

function getManufacturerViewCounts()
{
    $viewCounts = array();

    // Get all products
    $products = Mage::getModel('catalog/product')->getCollection();
    foreach ($products as $product) {
        // Assume you have a way to associate products with manufacturers using an attribute
        $manufacturerId = $product->getData('manufacturer');
        if (!isset($viewCounts[$manufacturerId])) {
            $viewCounts[$manufacturerId] = 0;
        }
        $viewCounts[$manufacturerId]++;
    }

    // Sort the view counts in descending order
    arsort($viewCounts);

    return $viewCounts;
}

This function retrieves all products and uses some assumed logic to associate each product with a manufacturer. It then updates an array of view counts for each manufacturer.

Updating Option Sort Order

Once you have your custom sorting criteria, you can update the sort order of the options using Magento’s resource model.

$viewCounts = getManufacturerViewCounts();
foreach ($optionsCollection as $option) {
    if (isset($viewCounts[$option['value']])) {
        // Set the new sort order based on the view count
        $option['sort_order'] = $viewCounts[$option['value']] * 10;
        Mage::getModel('catalog/resource_eav_attribute')->saveOptionValue($attribute, $option);
    }
}

In this code, we iterate through each option in our collection. For each option, if it matches one of the manufacturers from our view count array (found by matching the option['value'] with a manufacturer ID), we set its new sort order.

Note that we multiply the view count by 10 to ensure there are no conflicts if two manufacturers have the same view count. This is because Magento’s sort order values are integers and must be unique.

Conclusion

Updating the sort order of option dropdown labels in Magento programmatically can be a powerful way to optimize your store’s user experience or improve internal organization. By following this guide, you should now be equipped with the knowledge needed to accomplish this task using custom sorting criteria.

Remember, when working with Magento’s codebase, always ensure you have a full backup and are testing changes thoroughly before deploying them live.

For more advanced Magento development techniques, I recommend exploring the comprehensive resources available on LaravelCompany.

Happy coding!

Catalog Orders

Our network