Magento. Add order comment attribute to order

Magento. Add order comment attribute to order

October 2, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento. Add order comment attribute to order

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:

  1. It defines the new field k_customerordercomment with type ‘text’ and label ‘Comments’.
  2. It loops through entities (in this case, only ‘order’) to add the attribute using Mage_Eav_Model_Entity_Setup.
  3. It adds a column named k_customerordercomment of type ‘text NOT NULL DEFAULT ""’ to the sales_flat_order table.
  4. It creates an order attribute configuration array $c and passes it to addAttribute.

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_setup instead of sales 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:

  1. 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 ""'
);
  1. Update the entity setup line to use Mage_Sales_Model_Mysql4_Setup instead of core_setup:
$setup = new Mage_Sales_Model_Mysql4_Setup('sales_setup');
  1. 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
    ),
);
  1. 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:

  1. Use the correct method to set the value:
$order->setData('k_customerordercomment', 'Your custom comment here');
  1. Save the order:
$order->save();
  1. 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.

Magento 1 Database Admin Configuration

Our network