How to add Order Comment on Customer Checkout in Magento?

How to add Order Comment on Customer Checkout in Magento

This is by far one of the most popular question among the Magento users. Many users mentioned that a Remarks/Comment field was previously available in the Checkout page by default in the older Magento version. Here is an easy way to add the missing Remarks/Comment field in the customer checkout process by means of which the consumers can give their order comments without any trouble.

Add an EAV- Attribute to the Order Entity Model

Create a new local module or take an existing one, in which you place the needed changes/additions.

Create an EAV-Attribute for the order model by executing the php-code as shown below:

$c = array (
‘entity_type_id’  => 11,         // 11 is the id of the entity model “sales/order”. This could be different on
our system!

Look at database-table “eav_entity_type” for the correct ID!

‘attribute_code’  => ‘myorder_customercomment’,
‘backend_type’    => ‘text’,     // MySQL-DataType
‘frontend_input’  => ‘textarea’, // Type of the HTML-Form-Field
‘is_global’       => ‘1’,
‘is_visible’      => ‘1’,
‘is_required’     => ‘0’,
‘is_user_defined’ => ‘0’,
‘frontend_label’  => ‘Customer Comment’,
);
$attribute = new Mage_Eav_Model_Entity_Attribute();
$attribute->loadByCode($c[‘entity_type_id’], $c[‘attribute_code’])
->setStoreId(0)
->addData($c);
$attribute->save();

This code has to be executed in the proposed Magento environment once, so the eav attribute gets created. One option to do this is to create and run a PHP file like below:

<?php
require_once ‘app/Mage.php’;
umask(0);
Mage::app(‘default’);
// Add the code you want to execute here:

?>

integrate-magento-with-erp-appseconnect

Add a HTML text area field in one of the checkout templates

Overwrite a checkout template to add the text area field for remarks/comments. We did this in the file app/design/frontend/default/default/template/checkout/onepage/agreements.phtml. Place the new text area inside the form (opening and closing form tags) to be sure the variable is submitted. (Keep in mind that you have to “enable Terms and Conditions” in System->Configuration at Admin Panel if you want to place the text area in agreements.phtml. You can enable it through Magento Admin Panel: System-> Configuration-> Sales -> Checkout-> Checkout Options-> Enable Terms and Conditions)

<textarea name=”myCustomerOrderComment” id=”myCustomerOrderComment” style=”width:450px;
height:100px;”></textarea>

At Checkout Grab the submitted comment from the request variables

After the customer placed the order, capture the submitted comment from the request variables and call the automatically created setter for the order comment attribute, we did this with a helper-class which is called by the event checkout_type_onepage_save_order when placing the order.

For this you will need an xml-configuration in your local module (app/code/local/MyCompany/MyOrder/etc/config.xml):

<?xml version=”1.0″?>
<config>
<global>
<modules>
<MyCompany_MyOrder>
<version>0.1.0</version>
</MyCompany_MyOrder>
</modules>

<helpers>
<myorder>
<class>MyCompany_MyOrder_Helper</class>
</myorder>
</helpers>
</global>

<frontend>
<events>
<checkout_type_onepage_save_order>
<observers>
<myorder_set_customerordercomment>
<type>model</type>
<class>MyCompany_MyOrder_Helper_CustomerOrderComment</class>
<method>setCustomerOrderComment</method>
</myorder_set_customerordercomment>
</observers>
</checkout_type_onepage_save_order>
</events>
</frontend>
</config>

And you also need the helper class (app/code/local/MyCompany/MyOrder/Helper/CustomerOrderComment.php):

<?php
class MyCompany_MyOrder_Helper_CustomerOrderComment extends Mage_Core_Helper_Abstract
{
public function setCustomerOrderComment($observer)
{
$orderComment = $this->_getRequest()->getPost(‘myCustomerOrderComment’, false);
$observer->getEvent()->getOrder()->setMyorderCustomercomment($orderComment);
}
}

That’s all you require to save the comment automatically in Magento. Now as a last step all you have to do is to customize the templates where you like the comment to be shown (don’t forget to escape the data with something like htmlspecialchars()):

<?php
// If there is an order object:
echo $_order->getMyorderCustomercomment();
// If you only have the order-id (e.g. 11), you have to create an instance first:
$_order = Mage::getModel(‘sales/order’)->load(11);
echo $_order->getMyorderCustomercomment();
?>

 

integrate-magento-with-erp-appseconnect

You may also like:
Magento 2.0 Advanced Pricing
Magento’s New Partnership with Adobe
Top ERP Packages to Integrate with Magento

Subhajit Goswami

Subhajit is a Business Development Professional with ability in managing Customer Accounts and Channel Business. His specific interest is in the areas of Retail Industry and eCommerce, Multichannel eRetail and ERP Systems.

  • leandro

    Didn’t work Magento 1.9

  • leandro

    Didn’t work Magento 1.9

  • Hi Leandro,
    Customer can add order comment easily in checkout page with One step checkout from

    http://www.magentocommerce.com/magento-connect/one-step-checkout-22.html

  • John Th.

    Hi Leandro,
    Customer can add order comment easily in checkout page with One step checkout from

    http://www.magentocommerce.com/magento-connect/one-step-checkout-22.html

  • mage dev

    Hello, it gives error when we save try to submit the order, in FireBug (Mozilla FireFox) it gives below error (we are using magento 1.8)

    Fatal error: Call to a member function getSource() on boolean in C:xampphtdocsmy_storeappcodecoreMageCatalogModelProduct.php on line 1390

  • mage dev

    Hello, it gives error when we save try to submit the order, in FireBug (Mozilla FireFox) it gives below error (we are using magento 1.8)

    Fatal error: Call to a member function getSource() on boolean in C:xampphtdocsmy_storeappcodecoreMageCatalogModelProduct.php on line 1390

  • mage dev

    stop promoting paid extensions 🙂

  • mage dev

    stop promoting paid extensions 🙂

  • Interesting topic, thanks for the author, Subhajit Goswami!

    I believe that, the adding comment on order is very required and that also is an important features on checkout process. I also would love to suggest one: https://www.magentocommerce.com/magento-connect/one-step-checkout-37-28858.html
    It is never easier to help customer add their comment on Order.

    Hope it is useful for others.

  • redzwan

    This is for One Page Checkout, how about for Multi Shipping Check out? Is there any tweak or modification for this option? Thank you

  • Rashmi

    How to run given php code for EAV attribute?????

    • Biplab Rout

      You can run the code by creating any test php file uploading to the root and executing it. See the post above its mentioned.
      After creation you can check with attribute_code “myorder_customercomment” in the ‘eav_attribute’ table if the file executed properly.

  • Endre Szabó

    Ive done everything just as you said, the helper gots invoked, the attribute is saved, but when i call “getMyorderCustomercomment” gives NULL back :/ Any idea how to debug?