Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento - Add custom attribute to order

I'm trying to add a custom field to my orders. At this moment, I found the post bellow that helped me to create such attribute in my database: http://fabrizioballiano.net/2011/11/15/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();

After executing the code above and creating several orders, I'm able to loop through all orders and see the default value to the every order.

The question is, how can I store the data I want in this field? How can I retrieve such data?

Thanks!

like image 370
MatheusJardimB Avatar asked Jul 11 '13 17:07

MatheusJardimB


1 Answers

Add this to the gobal scope in config.xml. Then simply set the attribute in the quote - it gets automagically transferred to the order in the quote to order conversion process.

<global>
...
    <fieldsets>
        <sales_convert_quote>
            <your_special_attribute>
                <to_order>*</to_order>
            </your_special_attribute>
        </sales_convert_quote>
    </fieldsets>
...
</global>

You can retrieve/set the attribute at any time via the magic getter/setter e.g.

$quote->getYourSpecialAttribute()
$order->getYourSpecialAttribute()

$quote->setYourSpecialAttribute()
like image 156
Michael Leiss Avatar answered Nov 10 '22 08:11

Michael Leiss