Adding Custom Order Attributes to Magento Admin
In this post, we’ll address a common issue faced by developersâadding custom order attributes to the new order form in the Magento admin panel. We’ll provide step-by-step guidance on how to achieve this correctly.
Understanding the Problem
You’ve already created a custom order attribute named myattr and integrated it into the onepage checkout page. Now, you’re trying to display this attribute on the new order form in the admin interface. However, several attempts to retrieve the attribute have failed using various methods.
Let’s first look at the existing code snippets provided:
-
Attribute Creation:
$installer->addAttribute('order', 'myattr', $attribute); -
Initial Attempts to Retrieve Attribute:
- Using EAV configuration:
$eav = Mage::getModel('eav/config'); $attr = $eav->getAttribute('sales/order', 'myattr'); - Filtering by entity type ID:
$entityTypeId = $entityType->getEntityTypeId(); $attribute = Mage::getResourceModel('eav/entity_attribute_collection') ->setCodeFilter('myattr') ->setEntityTypeFilter($entityTypeId) ->getFirstItem(); - Directly accessing via order resource:
$order = Mage::getResourceSingleton('sales/order'); $myAttr = $order->getAttribute('myattr');
- Using EAV configuration:
As evident, none of these methods are retrieving the custom attribute correctly.
The Correct Approach
To add a new field for your custom attribute in the admin order form, you need to extend the Mage_Adminhtml_Block_Sales_Order_Create_Form_Account block and override its _prepareForm() method. Here’s how:
-
Create a new block:
First, create a new block that extends the base block:
class MyCompany_Mymodule_Block_Adminhtml_Sales_Order_Create_Form_Myattr extends Mage_Adminhtml_Block_Sales_Order_Create_Form_Account { // ... } -
Override _prepareForm() method:
In your new block, override the
_prepareForm()method to add a field for your custom attribute:protected function _prepareForm() { $form = parent::_prepareForm(); $fieldset = $form->getElement('account'); $fieldset->addField('myattr', 'text', array( 'label' => Mage::helper('mymodule')->__('My Custom Attribute'), // Add other field configuration as needed )); return $form; } -
Update the config XML:
Update your module’s
config.xmlfile to set the new block as an adminhtml layout update:<adminhtml> <layout> <updates> <mymodule> <file>sales/order/new/payment.phtml</file> <layout>/path/to/your/template.phtml</layout> <type>extends</type> </mymodule> </updates> </layout> </adminhtml> -
Retrieve the custom attribute:
To correctly retrieve your custom attribute, use the
Mage_Sales_Model_Ordermodel:$order = Mage::getModel('sales/order')->loadByIncrementId($incrementId); $customAttributeValue = $order->getData('myattr');
Best Practices and Tips
- Always ensure that your custom attributes are properly defined in the EAV setup. Use the
catalog_eav_attributetable to manage them. - When creating a new field in the
_prepareForm()method, make sure to include all necessary field configurations (e.g., label, input type, required flag). - Remember to clear cache and re-index after updating your module’s configuration.
Conclusion
Adding custom order attributes to the Magento admin interface requires careful control over blocks and layouts. By extending the appropriate block (Mage_Adminhtml_Block_Sales_Order_Create_Form_Account) and properly configuring your module in config.xml, you can seamlessly integrate your custom attributes into the admin order creation process.
If you’re looking to learn more about advanced Magento development techniques, be sure to check out our comprehensive tutorials at Laravel Company. We provide detailed guides on topics ranging from custom theme development to complex extension creation.