Solving Magento Custom Order Attribute Saving Issues
Hello developers! Today, we’re going to tackle an issue that many of us have encountered while working with custom attributes in Magento. The problem: we’ve added a custom attribute to the order entity, but it simply won’t save to the database.
The Problem at Hand
Our user has set up a new module and configured it correctly in config.xml:
<resources>
<hearedfrom_setup>
<setup>
<module>Brainworx_Hearedfrom</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</hearedfrom_setup>
</resources>
The MySqlInstall method looks fine too:
$attribute = array(
'type' => 'text',
'label' => 'Hearedfrom',
'visible' => true,
'required' => false,
'user_defined' => true,
'searchable' => true,
'filterable' => true,
'comparable' => false,
);
Yet, when trying to save the order with the new hearedfrom attribute, it just won’t stick. The user has implemented an observer:
public function hookToOrderSaveEvent()
{
// ... (loading the order object)
$order->setData(self::ORDER_ATTRIBUTE_FHC_ID, $_hearedfrom_data);
Mage::log("test: " . $order->getHearedfrom());
$order->save();
}
The log shows that the value is set and retrieved correctly. However, upon reloading the order, the custom attribute vanishes without a trace.
The Missing Piece
The issue lies in how Magento handles EAV (Entity-Attribute-Value) data. When you add an attribute to the order entity, Magento expects it to be saved in the eav_entity_text table. However, our configuration is incomplete.
We’ve installed the attribute correctly, but we haven’t defined the frontend input type or backend model. Without these definitions, Magento doesn’t know how to handle the data when saving the order.
The Solution
To resolve this issue, we need to update our config.xml file and add the missing definitions:
<resources>
<hearedfrom_setup>
<setup>
<module>Brainworx_Hearedfrom</module>
<class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</hearedfrom_setup>
<!-- Add this section -->
<order_hearedfrom>
<setup>
<module>Brainworx_Hearedfrom</module>
<class>Mage_Eav_Model_Entity_Type_SetUp</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</order_hearedfrom>
</resources>
Next, we need to update our MySqlInstall method:
$installer->startSetup();
$sql = "SELECT entity_type_id FROM " . $this->getTable('eav_entity_type') . " WHERE entity_type_code='order'";
$row = Mage::getSingleton('core/resource')->getConnection('core_read')->fetchRow($sql);
$attribute = array(
// ... (our existing attribute definitions)
'frontend_input' => 'text',
'backend_model' => 'eav/entity_attribute_backend_text',
);
// Add this line
$installer->addAttribute($row['entity_type_id'], 'hearedfrom', $attribute);
$installer->endSetup();
In the updated code, we’ve added the frontend_input and backend_model definitions. This tells Magento to use a text field for the frontend input and the default EAV text backend model.
Conclusion
When working with custom attributes in Magento, it’s crucial to define both the frontend input type and the backend model. Without these definitions, Magento won’t know how to handle the data when saving the entity. Always remember to update your config.xml file accordingly and ensure that all required setup resources are defined.
For more information on Magento EAV attribute configuration, feel free to check out our comprehensive guide at https://laravelcompany.com/magento-eav-attributes-guide.
Happy coding! Let me know if you have any questions.