Adding Customer Comments to Magento Checkout
Hello! In this post, I’ll guide you through adding customer comments to the checkout process in your Magento store. This will allow your customers to leave notes or special instructions with their orders.
Understanding the Issue
Your current setup seems straightforward - a block is being loaded at the checkout.xml file. However, the text field for the comment isn’t showing up during checkout.
You suspect that another extension (MageSetup) might be causing this issue by overriding the layout and loading different .phtml files from its own folder instead of using the configured layout in checkout.xml.
Checking if the Script Was Executed Correctly
Let’s first ensure that the script you found on AppSeConnect was executed correctly.
The script creates a new attribute called ‘myorder_customercomment’ and sets it to be visible and required. However, it seems there are some missing pieces in your current implementation.
Implementing the Solution
Here’s how you can properly implement this feature:
Step 1: Create the Attribute
Create an EAV (Entity-Attribute-Value) attribute for the customer order comment. Here’s the corrected script:
$c = array (
'entity_type_id' => 4, // Mage_Eav_Model_Entity_Type::ENTITY_TYPE_CUSTOMER
'attribute_code' => 'myorder_customercomment',
'backend_type' => 'text',
'frontend_input' => 'textarea',
'is_global' => 1,
'is_visible' => 1,
'is_required' => 0,
'frontend_label' => 'Customer Comment',
);
$attribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'myorder_customercomment');
if (!$attribute->exists()) {
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('customer', 'myorder_customercomment', $c);
}
Step 2: Save the Comment to the Order
Create a helper class to save the comment to the order when it’s placed:
class MyCompany_MyOrder_Helper_CustomerOrderComment extends Mage_Core_Helper_Abstract {
public function setCustomerOrderComment($observer) {
$order = $observer->getEvent()->getQuote();
if ($order && $order instanceof Mage_Sales_Model_Quote) {
$orderComment = $this->_getRequest()->getPost('myCustomerOrderComment', false);
$order->setMyorderCustomercomment($orderComment);
// Save the order with the comment
try {
$order->save();
} catch (Exception $e) {
Mage::logException($e);
}
}
}
}
Step 3: Set Up the Observer
Configure your module to observe the checkout_type_onepage_save_order event:
<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>
Conclusion
Adding customer comments to the Magento checkout involves a combination of creating an EAV attribute, saving it to the order using a helper class, and setting up an observer to trigger this action during checkout.
Remember to clear your cache after making these changes. If you still encounter issues, double-check that the MyCompany_MyOrder module is enabled in Magento’s admin interface under System > Configuration > Advanced > Disable Modules Output.
For more advanced customizations like this, I recommend checking out our comprehensive guides on Magento development at Laravel Company. We’ve covered everything from theme development to complex extension creation.