add custom order attribute to the order from in magento admin
I've added a custom order attribute and updated the onepage checkout page. Now I'm trying to add this attribute to the new order form in the admin. I'm trying to extend Mage_Adminhtml_Block_Sales_Order_Create_Form_Account and add a new field in the _prepareForm() method similar to the way the Group and Email fields are added.
How do I get the order attribute? I've tried several ways but nothing works. This is how I'm creating the custom order attribute in the mysql-install file:
$attribute = array(
'type' => 'int',
'label' => 'myattr',
'visible' => false,
'required' => false,
'user_defined' => false,
'searchable' => false,
'filterable' => false,
'comparable' => false,
);
$installer->addAttribute('order', 'myattr', $attribute);
and this is how I'm trying to get the attribute:
$res = Mage::getSingleton('core/resource');
$eav = Mage::getModel('eav/config');
$attr = $eav->getAttribute('sales/order', 'myattr');
or with this:
$entityType = Mage::getModel('eav/config')->getEntityType('order');
$entityTypeId = $entityType->getEntityTypeId();
$attribute = Mage::getResourceModel('eav/entity_attribute_collection')
->setCodeFilter('myattr')
->setEntityTypeFilter($entityTypeId)
->getFirs开发者_StackOverflow中文版tItem();
or this:
$order = Mage::getResourceSingleton('sales/order');
$myAttr = $order->getAttribute('myattr');
None of them work.
Have you verified that the attribute is being added to eav_attribute
table in the database with the correct entity_type_id
? (I think sales_order is 11 by default, but don't assume that)
At first glance, it looks like you should be using
$installer->addAttribute('sales/order', 'myattr', $attribute);
HTH, JD
Sales/Order used to use the EAV Model which supports attributes, that was before 1.4.0 or so not sure. I think now you should do:
$installer->getConnection()->addColumn($installer->getTable('sales/order'), 'my_column', 'decimal(12,4)');
you can still add the attribute as static field
$installer->addAttribute('order', 'my_column', array('type'=>'static'));
What I noticed after much trial-and-error was that the new attribute must have a default (non-null) value in order to work. Somehow the attribute is not writable if it has a 'NULL' value in the database. So using this attribute options array worked for me:
$attribute = array(
'type' => 'int',
'label' => 'myattr',
'default' => 0,
'visible' => false,
'required' => false,
'user_defined' => true,
'searchable' => false,
'filterable' => false,
'comparable' => false );
精彩评论