Adding Customer Comments to Magento Order Emails
In this guide, we’ll show you how to add customer comments from the one-step checkout process to the order confirmation email in your Magento store. The iwd_onepagecheckout extension allows customers to add notes which are currently not included in the automatic order emails sent to both customers and administrators.
Understanding the Order Status History Table
First, let’s understand where these customer comments are stored. By default, all additional information about an order is saved in the sales_flat_order_status_history table. This includes order status changes and any custom notes added by the customer using extensions like iwd_onepagecheckout.
The relevant columns for us are:
entity_id: The unique identifier for each status history record.parent_id: Refers to the parent order ID.is_customer_notifiedandis_visible_on_front: Both of these flags determine whether this particular status update should be visible to the customer or not. They are set to 1 by default in case you want to display all updates on the frontend.comment: Contains the actual comment or note added by the customer during checkout.
Adding Customer Comments to Order Emails
To include these customer comments in your order emails, we need to modify the email templates and observers that handle these emails. Here’s a step-by-step guide:
Step 1: Create a Custom Email Template
Go to App\Design\Adminhtml folder and create a new directory structure: frontend/MagentoTheme/email/order_new.html. This is where your custom order confirmation template will reside.
Add the following code in this file:
{{template config_path="design/email/header_template"}}
{{depend order.getCustomerComment()}}
<tr>
<td style="padding: 15px; border-collapse: collapse; border: 0;">
<h2 style="{{config path='design/email/header_font_size'}}">Customer Comment:</h2>
<p style="color: {{config path='design/email/body_text_color'}}, font-size: {{config path='design/email/body_font_size'}}, line-height: {{config path='design/email/line_height'}}, font-family: {{config path='design/email/body_font_family'}};">
{{var order.getCustomerComment()}}
</p>
</td>
</tr>
{{/depend}}
<!-- Rest of the template remains unchanged -->
This code adds a new table to your email template that includes the customer comment if one exists.
Step 2: Modify the Observer
The order confirmation email is sent by an observer located at Mage_Sales_Model_Order::_notifyCustomer. We need to update this model to include our custom comment in the email.
Open app/code/core/Mage/Sales/Model/Order.php and add the following function:
public function getCustomerComment()
{
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$select = $readConnection->select()
->from($resource->getTableName('sales_flat_order_status_history'))
->where('parent_id = ?', $this->getId())
->order('created_at DESC')
->limit(1);
$result = $readConnection->fetchRow($select);
if ($result && isset($result['comment'])) {
return trim(strip_tags(htmlspecialchars_decode($result['comment'])));
}
return '';
}
This function queries the sales_flat_order_status_history table for the most recent comment associated with the current order and returns it.
Conclusion
By following this guide, you should now be able to include customer comments from one-step checkout in your Magento order confirmation emails. This enhances communication between customers and administrators by providing more context about each order within the email itself.
Remember to always backup your files before making any modifications, especially when working with core files. If you find yourself needing expert help or want to learn more advanced techniques for customizing Magento, check out our professional services at Laravel Company.
Happy coding!