1

I want to add one attribute to sales/order, that is the Mage_Sales_Model_Order, to store some extra data.

I found the resource of sales/order uses normal table. And when I save an order, it only saves the fields that match the columns in the table.

What is the right way to add this attribute?

BlueWanderer
  • 2,671
  • 2
  • 21
  • 36
  • 2
    http://stackoverflow.com/questions/4378394/magento-adding-a-new-column-to-sales-flat-quote-item-and-sales-flat-order-item May help lead you in the proper direction. – B00MER Oct 23 '12 at 14:58

2 Answers2

3

I would recommend a hybrid of a few of the answers below. You definitly want to put this code in a install script via config xml but your going to want to use the Mage_Eav_Model_Entity_Setup as your setup class if you want to leverage the addAttribute function. So your config xml will look something like this...

<config>
...
    <resources>
        <modulename_setup>
            <setup>
                <module>Your_Module</module>
                <class>Mage_Eav_Model_Entity_Setup</class>
            </setup>               
        </modulename_setup>           
    </resources>
...
</config>

and your install script should look something like this

$installer = $this;
/* @var $installer Mage_Catalog_Model_Resource_Eav_Mysql4_Setup */

$installer->startSetup();

$installer->addAttribute('sales_order', 'attributename', array(
            'group'             => 'General',
            'label'             => 'Label frontend',
            'note'              => '',
            'type'              => 'string',    //backend_type
            'input'             => 'text', //frontend_input
            'frontend_class'    => '',
            'source'            => '',
            'backend'           => '',
            'frontend'          => '',
            'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
            'required'          => false,
            'visible_on_front'  => false,
            'apply_to'          => 'simple',
            'is_configurable'   => false,
            'used_in_product_listing'   => false,
            'sort_order'        => 5,
        ));

$installer->endSetup();

Keep in mind this is untested code, and you might need to tweak some of the attribute options to get it to function the way you need it to.

Steven Zurek
  • 535
  • 5
  • 18
0

To add new attribute on order you use this code, but just one time. Get any file and put the code there, execute one time and can remove the code.

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

        $setup->addAttribute('sales_order', 'attributename', array(
            'group'             => 'General',
            'label'             => 'Label frontend',
            'note'              => '',
            'type'              => 'string',    //backend_type
            'input'             => 'text', //frontend_input
            'frontend_class'    => '',
            'source'            => '',
            'backend'           => '',
            'frontend'          => '',
            'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_WEBSITE,
            'required'          => false,
            'visible_on_front'  => false,
            'apply_to'          => 'simple',
            'is_configurable'   => false,
            'used_in_product_listing'   => false,
            'sort_order'        => 5,
        ));
Guerra
  • 2,792
  • 1
  • 22
  • 32
  • I hope I don't have to precise that this code must be in an upgrade script (and so, without an explicit instantication of the Setup class) ... – Jscti Oct 23 '12 at 19:52
  • @Bixi i know that isn't the best way or the right way to do that. BUT works, without problems, i've already tried. but you'r right, this script is best on upgrade script but work anyplace. – Guerra Oct 23 '12 at 20:12
  • 1
    Yeah it works but it's a one time script... Magento offers ways to do these kind of automatics DB install/upgrade (see the response of Miss Magenta). You should modifiy your database ONLY in upgrade scripts.. for persistance and version control – Jscti Oct 23 '12 at 20:41