Reputation: 231
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..
Upvotes: 1
Views: 5582
Reputation: 231
Thanks clockworkgeek for your replies. I found the solution by using some extra address attributes instead because it was a lot more easier (i had to search a lot though).
so the solution is..
<?php
//Attribute to add
$newAttributeName = "rfc"; //modify this with the name of your attribute
//a) Add EAV Attributes (modify as you needed)
$attribute = array(
'type' => 'varchar',
'label' => 'RFC',
'visible' => true,
'required' => false,
'user_defined' => true,
'searchable' => false,
'filterable' => false,
'comparable' => false,
);
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
//Add to customer
$setup->addAttribute('customer_address', $newAttributeName, $attribute);
/* this is not working for some reason. add the columns manually
//b) Add Quote attributes (one page step to step save field)
$setup = new Mage_Sales_Model_Mysql4_Setup('sales_setup');
$setup->getConnection()->addColumn(
$setup->getTable('sales_flat_quote_address'),
$newAttributeName,
'text NULL DEFAULT NULL'
);
$setup->getConnection()->addColumn(
$setup->getTable('sales_flat_order_address'),
$newAttributeName,
'text NULL DEFAULT NULL'
);
*/
$eavConfig = Mage::getSingleton('eav/config');
$attribute = $eavConfig->getAttribute('customer_address', $newAttributeName);
$attribute->setData('used_in_forms', array('adminhtml_customer_address',
'adminhtml_checkout_address')); //'customer_register_address', 'customer_address_edit',
$attribute->save();
?>
and then edit the files according to this post. Magento: save custom address attribute in checkout
Upvotes: 1
Reputation: 37700
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.
Upvotes: 3