1

I had this issue:

I have created a custom mail where I send a list of products. For this in my controller i concatenated a variable like this:

foreach (.....){
    $orderItems .= "<tr>
      <td>
        <p>" . $productSku . "</p>
        <p>" .  $differanceQty . "</p>                         
      </td>
    </tr>";
}

When I tried to send this variable from my controller to the email template in my email I received the HTML code instead of only the value. So my custom email doesn't interpret my variable in the right way.

So I started looking for an answer and I didn't find anything related to this issue so that's why I posted this, maybe this will help someone.

the solution is to use the variable like this in the mail :

{{var order}} insead of {{htmlescape var=$order}}

2 Answers 2

1

The reason why you can get the variable {{var order}} is because you have the possibility to setTemplateParams() where you specify an array with params. This can be seen in Mage_Core_Model_Email_Template_Mailer:147

public function setTemplateParams(array $templateParams)
{
    return $this->setData('template_params', $templateParams);
}

Then the class core/email_template sends the array of params via its method sendTransactional() and that is why the custom variables are working for you.

0

This depends on the email that you want to send and the variable that you need, for exemple for the sales order email the function responsible is sendNewOrderEmail()located in : app/code/core/Mage/Sales/Model/Order.php and if you want to add some params in your case variable, you should rewrite this file in local (it's forbidden to update the core Magento files) I think you already know.

Solution 1: (Model)

So i'll give an exemple to well understand how to add your custom variable.

For the new order email template:

After rewritting the function in your locale codePool, you can add you new variable like this:

public function sendNewOrderEmail()
{
/*.
  .
  . there is some Magento code here*/

    // it's here where you have to add a variable
    $myValue = Mage::getModel('customer/customer')->load($this->getMyValue()); // for exemple
    $mailer->setTemplateParams(array(
      'order'         => $this,
      'billing'       => $this->getBillingAddress(),
      'payment_html'  => $paymentBlockHtml,
      'new_variable'  => $myValue // your new param here (variable)
   ));
   /*Rest of the code remains the same*/
 }

Now you can get your variable in the this template email like this: {{var new_variable}}

Solution 2: (phtml)

You add your variable in some phtml that you get it in the email template like this:

{{block type='core/template' area='frontend' template='page/html/email/var1.phtml' order=$order}}

And you add your variable value in this phtml like this:

<?php 
 $customer_id = 10;
 $myValue = Mage::getModel('customer/customer')->load($customer_id);
 echo $myValue->getMyValue() ; 
?> // for exemple

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.