Enhancing Magento Order Emails: Adding Customer Company Name to Subject
Magento is an incredibly flexible e-commerce platform that allows for a wide range of customizations. One common request from merchants is to include the customer’s company name in the subject line of new order emails sent by their store. This can help improve the relevance and importance of these messages.
Understanding the Default Behavior
By default, Magento sends order-related emails using predefined templates and subject lines. For new orders, the subject typically follows this format:
Order #%s from %customer_name% on %date%
As you can see, this default setup includes the order ID, customer name, and date of creation. However, it does not account for any additional details like company names.
Adding Company Name to Order Emails
To add the customer’s company name to new order email subjects in Magento 1.x, we’ll need to modify a core template file and an observer. This involves a bit of code editing and some configuration changes.
Step 1: Modify the Email Template
The first step is to update the order.new.html template that contains the subject line for new orders. You can find this file at app/design/frontend/base/default/template/transactional/email/order/new.html.
Open this file in a text editor and locate the <subject> tag near the top of the document:
<subject><![CDATA[Order #%s from %customer_name% on %date%]]></subject>
To include the customer’s company name, you can modify it like so:
<subject><![CDATA[Order #%s from %company_name% by %customer_name% on %date%]]></subject>
Just replace %company_name% with {$billing.address.company} to pull in the billing address’s company name.
Step 2: Update the Email Observer
Now, we need to ensure that Magento uses our modified template for new orders. To do this, we’ll update the observer responsible for sending order emails.
Locate the file app/code/core/Mage/Sales/Model/Order.php and look for the addEmailIdsToTrigger() method:
public function addEmailIdsToTrigger($observer)
{
$order = $observer->getEvent()->getOrder();
if ($order->getId()) {
// ... (other code)
$emailTemplateModel = Mage::getModel('core/email_template');
$variables = array(
'order' => $order,
'billing' => $order->getBillingAddress(),
'shipping' => $order->getShippingAddress()
);
foreach ($templates as $type => $template) {
if (empty($template['subject'])) {
$emailTemplateModel->setDesignConfig(false);
$template['subject'] = Mage::helper('sales')->__(
$emailTemplateModel->getMailSubject($variables)
);
}
}
}
}
In the foreach loop, you’ll see that we’re setting the subject for each email template. However, this code doesn’t actually send the emails; it just prepares them. To ensure our changes take effect, we need to extend this functionality.
Create a new observer module at app/code/local/YourCompany/EmailSubject/. Inside the app/code/local/YourCompany/EmailSubject/etc/ folder, create an XML configuration file called config.xml:
<?xml version="1.0"?>
<config>
<modules>
<YourCompany_EmailSubject>
<version>0.1.0</version>
</YourCompany_EmailSubject>
</modules>
<global>
<models>
<yourcompany_emailsubject>
<class>YourCompany_EmailSubject_Model</class>
<resourceModel>yourcompany_emailsubject_resource</resourceModel>
</yourcompany_emailsubject>
</models>
<resources>
<yourcompany_emailsubject_setup>
<setup>
<module>YourCompany_EmailSubject</module>
</setup>
</yourcompany_emailsubject_setup>
</resources>
</global>
<frontend>
<events>
<sales_order_save_after>
<observers>
<yourcompany_emailsubject>
<type>singleton</type>
<class>YourCompany_EmailSubject_Model_Observer</class>
<method>sendOrderEmails</method>
</yourcompany_emailsubject>
</observers>
</sales_order_save_after>
</events>
</frontend>
</config>
This configuration file defines a new module and an observer that will trigger after each order is saved.
Next, create the observer class at app/code/local/YourCompany/EmailSubject/Model/Observer.php:
class YourCompany_EmailSubject_Model_Observer
{
public function sendOrderEmails($observer)
{
$order = $observer->getEvent()->getOrder();
if ($order->getId()) {
$emailTemplateModel = Mage::getModel('core/email_template');
$variables = array(
'order' => $order,
'billing' => $order->getBillingAddress(),
'shipping' => $order->getShippingAddress()
);
foreach ($templates as $type => $template) {
if (empty($template['subject'])) {
$emailTemplateModel->setDesignConfig(false);
$template['subject'] = Mage::helper('sales')->__(
$emailTemplateModel->getMailSubject($variables)
);
}
}
// Now we actually send the emails
Mage::helper('sales')->sendOrdersEmails($order, array_keys($templates));
}
}
}
This observer calls Mage::helper('sales')->sendOrdersEmails() to dispatch the prepared email templates.
Conclusion
Adding a customer’s company name to new order email subjects in Magento requires a combination of template modifications and observer extensions. By updating the order.new.html file and creating a custom observer, we can ensure that the subject line includes this additional information.
Remember, these changes are specific to Magento 1.x versions. If you’re using a later version or an alternative platform like Laravel (available from LaravelCompany), the process may differ slightly due to architectural updates and changes in the core functionality.
Always test your customizations thoroughly before deploying them to a production environment, and consider using a backup solution to safeguard your data. Happy customizing!