Magento - How to create attribute programmatically that can be used in 'Sort by'

Magento - How to create attribute programmatically that can be used in 'Sort by'

October 18, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - How to create attribute programmatically that can be used in 'Sort by'

Magento: Programmatically Creating Attributes for ‘Sort by’ Functionality

In Magento, attributes play a crucial role in managing your product catalog’s metadata. However, creating these attributes programmatically can sometimes lead to unexpected behaviors, especially when it comes to the ‘Sort by’ functionality on the frontend.

Let me walk you through the process of creating an attribute that can be used for sorting products in the ‘Sort by’ dropdown menu.

Understanding Magento Attribute Properties

When creating an attribute using an upgrade script or programmatically, it’s essential to understand the various properties you need to set. Here’s a breakdown of some key properties:

  1. attribute_set and group: These determine where your attribute will appear in the back office. The Main attribute set is typically used for most product types.

  2. type: Specifies the data type of the attribute, which can be an integer (int), text (varchar), or a decimal (decimal).

  3. input: Defines how the attribute will be inputted in the back office. For textual fields, use text.

  4. label: The name that appears for the attribute field.

  5. global: Whether this attribute should be applied across all stores (1) or is specific to a single store (0).

  6. required, comparable, searchable, etc.: These are boolean flags that control various behaviors like whether an attribute is required, comparable for search purposes, and so on.

  7. used_for_sort_by: When set to 1, the attribute should theoretically be available in the ‘Sort by’ dropdown on the frontend.

The Issue with Upgrade Scripts

In your case, you’ve correctly defined most of the properties in your upgrade script. However, you’re encountering a problem where the used_for_sort_by flag isn’t working as expected in the frontend. This discrepancy often arises due to how Magento processes attribute data during installation.

Resolving the ‘Sort by’ Issue

To fix this issue without manually updating the attributes in the back office, you can use an event observer that gets triggered after the installation is complete or an upgrade script runs. Here’s a high-level approach:

  1. Create an event listener: Magento events provide a way for observers to react to specific actions like the post-install-cleanup or post-upgrade-events.

  2. Fetch your attribute: Use the Mage_Catalog_Model_Resource_Eav_Attribute class to retrieve your newly created attribute by its code or ID.

  3. Update the ‘Used for Sorting in Product Listing’ flag: You can use the setData() method to update this field to 1 (which means yes).

Here’s a basic example of what this might look like:

public function postInstallCleanup($observer)
{
    $attribute = Mage::getModel('catalog/resource_eav_attribute')
        ->loadByCode('catalog_product', 'your_attribute_code');

    if ($attribute->getId()) {
        $attribute->setData('used_in_product_listing', 1);
        $attribute->save();
    }
}

Remember to replace 'your_attribute_code' with the code of the attribute you created.

Why Other Properties Are Set to ‘No’

Regarding your edit, where other properties like required are set to ‘No’—this is likely due to how Magento processes the data during installation or upgrade. The data you provide in your script might not always be persisted exactly as intended. It’s a known issue that has been discussed extensively on the Magento forums.

Conclusion

Creating attributes programmatically in Magento can be a powerful tool when done correctly. However, it requires a solid understanding of how the system handles attribute metadata and events during installation or upgrade processes.

By using event observers to update your attributes after they’ve been created, you can ensure that they are configured properly for use in the ‘Sort by’ dropdown on the frontend without having to manually edit them in the back office.

Catalog Deployment SEO

Our network