May 10, 2013
kalpesh

Magento add attribute to order

Adding custom attribute to order in Magento is same as we do for customer and category. The difference is we will use different setup class AND we will not need attribute set, group and attribute input type now. We will create a quick module which will do exactly what we want and nothing more than that. So let’s start our new module.

1.) Create a file at app/etc/modules/ and name it whatever you want. I will name it Namespace_Module.xml
Paste this code in that file:

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Module>
            <active>true</active>
            <codePool>local</codePool>
        </Namespace_Module>
    </modules>
</config>

2.) Create necessary directories to reach to app/code/local/Namespace/Module/etc/, so that we can create our config.xml there. Paste below code in this config file:

<?xml version="1.0"?>
<config>
    <modules>
        <Namespace_Module>
            <version>0.0.1</version>
        </Namespace_Module>
    </modules>
 
    <global>
        <resources>
            <modulename_setup>
                <setup>
                    <module>Namespace_Module</module>
                    <class>Mage_Sales_Model_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </modulename_setup>
            <modulename_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </modulename_write>
            <modulename_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </modulename_read>
        </resources>
    </global>
</config>


3.) Now comes our main file, which will create new Order attribute for us. Create file “mysql4-install-0.0.1.php” in app/code/local/Namespace/Module/sql/module_setup/, and paste the below code in it:

<?php
$this->startSetup();
$this->addAttribute('order', 'your_custom_attribute_here', array(
    'type'          => 'varchar',
    'label'         => 'Custom Order attribute label',
    'visible'       => true,
    'required'      => false,
    'visible_on_front' => true,
        'user_defined'  =>  true
));
 
$this->endSetup();

This will add new attribute to Order and after clearing cache you can see your custom attribute in backend “Manage Orders” screen and in customer frontend screen “My Orders”. HTH!

16 Comments

  • Hello. I’m trying to add a custom order attribute using your instructions above. I’ve followed your instructions exactly. The only things I changed were the names of the Namespace_Module, the name and label of the attribute, and I added a closing tag to your php script. I keep checking the table eav_attribute to see if the attribute is there, but nothing. I’ve also made sure to clear cache before refreshing. Finally, I don’t see the module version in the table core_resource, but I do see the module active on the Admin section of the backend. Are you sure your code is accurate? Have you tested it on CE 1.7? Thanks for the help!

  • Hi Bryan, Yes the code should work and you should see a new attribute on frontend and backend considering you did following modifications in above code:
    – Change Namespace_Module to your’s one.
    – Change module in config.xml to your module name
    – Change your_custom_attribute_here to your attribute’s name
    – Check the files and directories name and modify to meet your module’s name
    – You can try replacing Mysql4 to Resource in config.xml setup class to test if that works

    Clear cache, which you did. If still you can’t see your attribute I will suggest you to check in error logs to make sure there were no errors during the attribute creation process.

  • Thanks for the quick response. I’ve followed your instructions exactly, but it just isn’t working. No attribute is showing up in the frontend, backend, or the eav_attribute table. No error is showing up in the error logs, either.

  • Hello sir,i want to pass custom attribute value of order at the point of checkout and also visible in invoice and order how can we implement this please suggest?

  • Hi Kaplesh, have been going thru your blog & I must appreciate the work you have done, Its really good!. Keep going 🙂

    I was having a query regarding Order in Mgento,
    How to generate Sub Order ID, basically, if we wish to split the orders then how to do that ?
    For E.g.: we are having 4 products in a single order, we need to split our each product order by defining maybe a sub order ID or something that should resolve our purpose.

    Looking forward to hear from you.

  • Hi Kalpesh,
    Thanks for this tutorial.
    I followed the steps and see an entry of my module in the core_resource table. However I don’t see anything on the frontend or the backend and also in eav_attributes table.
    No errors either. I flushed the cache also. So not sure what else might be wrong.

    I am trying to add an attribute Order Incharge, which should be set in the admin screen.

    Please help.

    Thanks,
    Neet

  • Hi,
    I just checked my sales_flat_order table, and I see an entry for Order incharge in there and it has all null values for now.
    How do I get it to display on the Order details page, so I can update this field?

    Thanks,
    Neet

    • You will need to write the code for that. May be in app/design/adminhtml/default/default/template/sales/order/view/info.phtml or wherever you want it to appear.

  • Thank you Kalpesh. Worked like a treat 🙂

  • Hello, is there any way to get an attribute in the sales_order.info method too ? I need a costum attribute available in api call.

    Thanks for answering.

  • Thanks, Kalpesh! Though your tutorial is really good, it’s still very hard for me to make such changes programmatically. I prefer using extensions, for that I’m using order attributes by Amasty.

  • Hi Kalpesh ,
    I have added an attribute for size of rings its value can be 4,5,6,7,8, like wise. I just want to know how can i get the value that customer/user will select into order/invoice mail. so that we can recognize which size of ring user have ordered. suppose A user ve selected 5 as size so in mail of respective order should contain an information like Size:5

  • Just to help out – some of the comments here are saying that it’s not working. If you are not used to setting up a resource in a module it may not be obvious that when it says ‘app/code/local/Namespace/Module/sql/module_setup’ you have to replace Module with your module name but also replace module_setup with yourmodulename_setup too.

  • Hello,

    I want to create custom order attribute for store shipping method when order placed.

    what to do?

  • Hello,

    I am looking forward to add an attribute to order page under shipping section. The new custom attribute should be able to get the delivery date from customer as we deal in perishable items where delivery date is important.

    I found this extension – https://www.fmeextensions.com/magento-additional-checkout-attributes-fields.html

    Can you please provide a development solution or recommend some other extensions

  • Hello,

    May I get the screen shot of result?

Leave a comment

 

Welcome to my Blog

Kalpesh MehtaHelping Magento developers in their day-to-day development problems since 2011. Most of the problems and solutions here are my own experiences while working on different projects. Enjoy the blog and don't forget to throw comments and likes/+1's/tweets on posts you like. Thanks for visiting!

Certifications

Honor

Recognition

Magento top 50 contributors

Magento top 50 contributors

Contributions