Adding Customer Order Comments to Magento
In Magento 1.6, adding attributes like customer comments to orders requires careful setup of EAV (Entity Attribute Value) entities and tables. The provided script attempts to add an order comment attribute but encounters issues when setting the value.
Let’s break down the problem and provide a solution that properly sets up the order comment attribute.
Current Issues in the Setup Script
The current script has several sections:
- It defines the new field
k_customerordercommentwith type ‘text’ and label ‘Comments’. - It loops through entities (in this case, only ‘order’) to add the attribute using
Mage_Eav_Model_Entity_Setup. - It adds a column named
k_customerordercommentof type ‘text NOT NULL DEFAULT ""’ to thesales_flat_ordertable. - It creates an order attribute configuration array
$cand passes it toaddAttribute.
The main issues are:
- The script is using both EAV setup and direct database manipulation to add the column, which is unnecessary and can cause inconsistencies.
- The attribute creation uses the wrong entity setup (
core_setupinstead ofsales Setup). - The script doesn’t set any backend model for the attribute.
Correcting the Attribute Setup
To correctly add an order comment attribute, follow these steps:
- Remove or comment out the following lines that add the column directly to the database:
$installer->getConnection()->addColumn(
$installer->getTable('sales_flat_order'),
'k_customerordercomment',
'text NOT NULL DEFAULT ""'
);
- Update the entity setup line to use
Mage_Sales_Model_Mysql4_Setupinstead ofcore_setup:
$setup = new Mage_Sales_Model_Mysql4_Setup('sales_setup');
- Add a backend model and input type for the attribute:
$newFields = array(
'k_customerordercomment' => array(
'type' => 'text',
'label' => 'Comments',
'input' => 'textarea', // Change from 'text' to 'textarea'
'backend_model' => 'sales/order_attribute_backend_text', // Add this line
),
);
- The rest of the attribute configuration remains the same.
Setting the Attribute Value
To set the order comment value, make sure you follow these guidelines:
- Use the correct method to set the value:
$order->setData('k_customerordercomment', 'Your custom comment here');
- Save the order:
$order->save();
- If you want to load an existing order and see the comment, make sure the attribute is included in the load fields:
$order = Mage::getModel('sales/order')->loadByIncrementId($incrementId);
$order->loadAttribute('k_customerordercomment');
Conclusion
Adding customer comments to orders in Magento requires a careful setup of EAV entities and proper attribute configuration. The key is using the correct entity setup (Mage_Sales_Model_Mysql4_Setup) and setting up the backend model for the attribute.
Remember to test your script thoroughly, especially when modifying core files or adding attributes that store sensitive customer data.
For more advanced customizations in Magento, consider checking out Laravel Company’s Magento development services and Magento 1.x development resources.
By following these guidelines, you should be able to successfully add customer order comments in your Magento store.