Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom attribute in order email templates - Magento

Tags:

php

magento

I have created a 'Companyname' attribute which gets added up in my Customer's Account information and is a required field.

It gets filled up on registration, form and edit pages fine and gets displayed on Customer's Grid in the back-end too.

However I am not able to display the Company name in any of my order email templates.

I believe this is because there is neither any column called 'companyname' in my order tables nor do I have any custom variable which I can pass to order/invoice/shipment templates to display Company name right next to the line after Customer's name.

Can any one point out the file where I can create this custom variable containing my custom 'companyname' attribute and pass it to all types of sales email templates

Thanks

like image 538
ivn Avatar asked Jul 09 '12 10:07

ivn


People also ask

How do I change the order of email templates in Magento 2?

In order to manage the transactional emails in Magento 2 go to Admin Panel > Marketing > Communications > Email Templates. Then, choose an email template you want to edit or you can add a new one by pressing the Add New Template button.

How do I style an email template in Magento 2?

4 Steps to Customize Email Template in Magento 2On the Admin Panel, Marketing > Communications > Email Templates . Click on Add New Template , Choose the types of the Template: Header, Footer, or Message Templates. Click on Load Template , the corresponding format will appear.


1 Answers

After a little bit of searching I found the right file to make the changes. Since I already had 'companyname' as one of my attributes I retrieved the value of this field and passed it as a param in the following function

app/code/core/Mage/Sales/Model/Order.php

public function sendNewOrderEmail()
{
 /*Existing Code*/
 if ($this->getCustomerIsGuest()) {
        $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_GUEST_TEMPLATE, $storeId);
        $customerId = Mage::getModel('customer/customer')->load($this->getCustomerId());
        $companyname = $customerId->getCompanyname();
        $customerName = $this->getBillingAddress()->getName();
    } else {
        $templateId = Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE, $storeId);
        $customerId = Mage::getModel('customer/customer')->load($this->getCustomerId());
        $companyname = $customerId->getCompanyname();
        $customerName = $this->getCustomerName();
    }
    /*Existing Code*/
    $mailer->setTemplateParams(array(
      'order'        =>  $this,
      'billing'      =>  $this->getBillingAddress(),
      'payment_html' => $paymentBlockHtml,
      'companyname'  => $companyname
   ));
   /*Rest of the code remains the same*/
 }

After making this change. I edited my Transactional Email to include this param. Since I wanted to display inside Shipping Address, I placed my variable just before this line in

   System > Transactional Emails > New Order Email 

     {{ var companyname }}
     {{var order.getShippingAddress.format('html')}}

If your companyname is getting saved as a part of Customer Information then this would get displayed in your Order Email in 'Shipping Address' Information right at the Start.

You can do the same for Invoice and Shipment Emails.

Hope this helps someone !!! :-)

like image 105
ivn Avatar answered Oct 07 '22 06:10

ivn