Magento custom order attributes

attributesmagentoorders

i'm trying to add some custom attributes to the checkout(sales) page of magento 1.5.1.0 for orders created from the backend (administrator) panel.
I tried this code and i can see my new attribute in the eav_attribute table, but i can't see my new attribute when i make an order from the backend.
What am i missing..?

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->addAttribute('5', 'testattr', array(
 'label' => 'testlabel',
 'type' => 'varchar',
 'input' => 'text',
 'visible' => true,
 'required' => false,
 'position' => 5,
 ));


$eavConfig = Mage::getSingleton('eav/config');
$attribute = $eavConfig->getAttribute('5', 'testattr');
$attribute->setData('used_in_forms', array('checkout_register', 'adminhtml_checkout'));
$attribute->save(); 

Thanks..

Best Answer

Order entities are awkwardly not like normal entities, they use a flat table instead of EAV, which means that it's not enough to just assign an attribute. You must alter the flat table (which I tried here) or create additional tables then join them to the flat table. With hindsight I see I should have taken the second option, it's safer.


I've been looking a bit closer at the order template and there might a hackish sort of workaround via a lesser used block. To start with let's make changes in a layout/local.xml file so it's safe from overwriting by upgrades;

<layout>
    <adminhtml_sales_order_create_index>
        <reference name="gift_options">
            <block type="adminhtml/template" template="YOUR/TEMPLATE.phtml" />
        </reference>
    </adminhtml_sales_order_create_index>
</layout>

The gift options block is constructed in an open-ended way so adding to it is relatively easy. Obviously replace YOUR/TEMPLATE.phtml with the path of a file you will create. The template being inserted needs to have input fields with names like order[testattr] and those should be copied directly to the database table (according to Mage_Adminhtml_Model_Sales_Order_Create, if I am reading the source code right). The gift options block already creates a <fieldset> inside the order's <form> so only the input fields are needed.

Related Topic