Magento - Add custom attribute to order

Magento - Add custom attribute to order

July 24, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - Add custom attribute to order

Storing Custom Data for Magento Orders

As an experienced Magento developer, I often need to add custom attributes to orders to store specific data. Recently, I encountered a situation where I had created the attribute using the provided script but struggled with storing and retrieving the actual values.

Creating the Custom Order Attribute

Let’s start by reviewing how to create a custom order attribute in Magento:

require_once('app/Mage.php');
Mage::app()->setCurrentStore(Mage::getModel('core/store')->load(Mage_Core_Model_App::ADMIN_STORE_ID));

$installer = new Mage_Sales_Model_Mysql4_Setup;
$attribute  = array(
   'type'          => 'int',
   'backend_type'  => 'text',
   'frontend_input' => 'text',
   'is_user_defined' => true,
   'label'         => 'My Label',
   'visible'       => true,
   'required'      => false,
   'user_defined'  => true,
   'searchable'    => true,
   'filterable'    => true,
   'comparable'    => true,
   'default'       => 0
);
$installer->addAttribute('order', 'special_attribute', $attribute);
$installer->endSetup();

This code creates an order attribute named “special_attribute” with the specified configuration.

Storing Data in the Custom Attribute

Now, let’s store data in this custom attribute. You can achieve this by modifying your order creation process:

$order = Mage::getModel('sales/order');
$order->setData('special_attribute', 'Your Custom Value Here');
$order->save();

In this example, the “special_attribute” is set to “Your Custom Value Here”. Replace this with the actual data you want to store.

Retrieving Data from the Custom Attribute

To retrieve the stored value, you can loop through your orders:

$orders = Mage::getModel('sales/order')->getCollection();
foreach ($orders as $order) {
    echo 'Order ID: ' . $order->getId() . ', Special Attribute Value: ' . $order->getSpecialAttribute();
}

This code retrieves all the orders and prints the order IDs along with their “special_attribute” values.

Best Practices for Custom Order Attributes

As you work with custom order attributes, consider these best practices:

  1. Use descriptive names: Choose attribute names that clearly describe the type of data they store.
  2. Set appropriate defaults: Define reasonable default values for your attributes.
  3. Consider data types carefully: Ensure the chosen data type can accurately represent your data.

Remember to always test your changes thoroughly and backup your database before making modifications.

Conclusion

In this post, we’ve explored how to create custom order attributes in Magento, store data in these attributes, and retrieve that data for analysis or further processing. By following these steps, you can easily extend the functionality of Magento orders to suit your specific business needs.

For more detailed information on Magento development, check out our comprehensive guides at Laravel Company.

Database Admin Configuration Orders

Our network