Magento how to add new attribute to sales/order model?

Magento how to add new attribute to sales/order model?

July 30, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento how to add new attribute to sales/order model?

Adding Custom Attributes to Magento Sales/Orders

As an e-commerce developer, you may find yourself needing to store additional data within your Magento sales orders. This could be anything from custom order notes to tracking information that isn’t currently supported by the built-in fields.

Understanding the Magento Sales/Order Model

The Mage_Sales_Model_Order model in Magento represents a single order record. By default, this model maps directly to the sales_flat_order table in your database, which contains standard fields like entity_id, created_at, and other basic order details.

When an order is saved, only the fields that exist as columns in the sales_flat_order table are persisted to the database. Any additional data you want to store must be added as custom attributes.

Adding a New Attribute to Sales/Orders

To add a new attribute to the Mage_Sales_Model_Order, you’ll need to follow these steps:

1. Create an Eav Setup Script

The Magento Entity-Attribute-Value (EAV) model allows you to store custom data in a flexible way. To create a new order attribute, your first step is to define it using an eav_setup script.

Create a file named OrderSetup.php under the app/code/local/Magento/Setup directory with the following content:

<?php

class Magento_Setup extends Mage_Eav_Model_Entity_TypeSetup
{
    public function install()
    {
        $installer = new Mage_Eav_Model_Resource_Setup('core_setup');
        $installer->startSetup();

        // Define your custom attribute
        $installer->addAttribute(
            'order',
            'my_custom_attribute',
            array(
                'type' => 'varchar',
                'input' => 'text',
                'backend' => null,
                'frontend' => null,
                'label' => 'My Custom Attribute',
                'visible' => true
            )
        );

        $installer->endSetup();
    }
}

In this example, we’re creating a new attribute named my_custom_attribute of type varchar. You can customize the label, input type, and other settings as needed.

2. Run the Setup Script

To run the setup script, navigate to your Magento admin panel and go to System > Configuration > Advanced > Install/Uninstall tab. Click on the “Run All Setup Scripts” button to install any pending scripts, including yours.

3. Integrate the New Attribute into Your Code

With the attribute created, you can now use it in your custom code. To set or get the value of my_custom_attribute for a specific order:

$order = Mage::getModel('sales/order')->loadByIncrementId($incrementId);

$customAttributeValue = $order->getData('my_custom_attribute');

// Set the attribute value
$order->setData('my_custom_attribute', 'New Value');
$order->save();

Best Practices for Custom Attributes

When working with custom attributes, keep these best practices in mind:

  • Use a descriptive attribute name that reflects its purpose.
  • Choose an appropriate data type based on the expected value format (varchar, int, text, etc.).
  • Ensure your code handles cases where the attribute may not be available or is empty.
  • Regularly review and clean up unused custom attributes to keep your database optimized.

Conclusion

Adding a new attribute to Magento sales orders requires creating an EAV setup script, running it through the admin panel, and then integrating the attribute into your custom code. This approach allows you to extend the core order model without modifying the existing database schema directly.

Remember that working with custom attributes involves understanding the Entity-Attribute-Value (EAV) architecture of Magento’s data model. For a more comprehensive guide on Magento development practices, I recommend checking out Laravel Company, which offers in-depth tutorials and training resources for e-commerce developers.

With this knowledge, you’re now empowered to add custom attributes to your sales orders as needed, enabling greater flexibility and data storage capabilities within your Magento store.

Database Orders

Our network