Php – Add custom attribute in order email templates – Magento

magentoPHP

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

Best Answer

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 !!! :-)

Related Topic