1

I am trying to modify my email template to generate different product information based on customer group. As in, if a customer belongs to Group A, load modified.phtml, else load default.phtml.

Specifically, I am working with the New Order email template where the base transactional email template calls {{layout handle="sales_email_order_items" order=$order}}, which loads the template file in /app/design/frontend/rwd/default/template/email/order/items.phtml.

This template eventually calls echo $this->getItemHtml($_item).

From what I can tell, this loads the template file /app/design/frontend/rwd/default/template/email/order/items/order/default.phtml, which outputs the template information to print each item's content (SKU, Name, Price, etc) in the table.

This is what I'm trying to accomplish:

$customerGroupId    = $this->getCustomerGroupId();
$customerGroupName  = Mage::getModel('customer/group')->load($customerGroupId)->getCustomerGroupCode();
if($customerGroupName = 'A') {
    // How to getItemHtml from different template?
} else {
    echo $this->getItemHtml($_item)
}

I assume that I will need to add reference to my modified.phtml in the /app/design/frontend/rwd/default/layout/sales.xml file like so:

<sales_email_order_items>
    <block type="sales/order_email_items" name="items" template="email/order/items.phtml">
        <action method="addItemRender"><type>default</type><block>sales/order_email_items_order_default</block><template>email/order/items/order/default.phtml</template></action>
        // modified.phtml reference here?

Can I make getItemHtml specify a different template?

Or is there a different way I need to be approaching this?

I could replace the {{layout handle...}} with a {{block...}} call and put all of the template information within my if... else... blocks, but this seems very hacky. Any thoughts or suggestions?

  • I think you should have two different blocks with two templates in your page which you will add with xml reference file and then you will just call the appropriate block – Nickool Jul 10 '15 at 21:50
1

To change the itemHtml depends upon the customer group. You can achieve that in layout file using a helper method.

<sales_email_order_items> 
     <block type="sales/order_email_items" name="items" template="email/order/items.phtml">
         <action method="addItemRender"><type>default</type><block>sales/order_email_items_order_default</block><template helper="mymodule/data/myFunction"/></action>

Then create myFunction in your helper file

 app/code/{local|community}/yourNamespace/yourModule/helper/Data.php.

Use the below code

Update :

public function myFunction()
{
    $customerGroupName = '';
    if($customerGroupId = Mage::getSingleton('adminhtml/sales_order_create')->getQuote()->getCustomerGroupId()):
        $customerGroupName = Mage::getModel('customer/group')->load($customerGroupId)->getCustomerGroupCode();
    elseif(Mage::getSingleton('customer/session')->isLoggedIn()):
        $customerGroupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
        $customerGroupName = Mage::getModel('customer/group')->load($customerGroupId)->getCustomerGroupCode();
    endif;
    if($customerGroupName == 'General') { 
       return 'email/order/items/order/default.phtml'; 
    } else { 
      return 'email/order/items/order/another.phtml';
    }
 }

That's it.

| improve this answer | |
  • I got a Fatal error: Call to undefined method MyNamespace_MyModule_Helper_Data::getCustomerGroupId() in .../Helper/Data.php on line 5. Is it trying to find the method in my helper? – travisw Jul 13 '15 at 17:18
  • Create an custom module and load that function through that – MeenakshiSundaram R Jul 13 '15 at 17:34
  • I've created a custom Module named Emailbygroup. It appears to call the Helper correctly when an email is sent, but my helper acts like it is looking for a method getCustomerGroupId within my Emailbygroup/Helper/Data.php file. – travisw Jul 13 '15 at 17:37
  • Get customer group id from order – MeenakshiSundaram R Jul 13 '15 at 17:42
  • @travisw check by updated code. – MeenakshiSundaram R Jul 14 '15 at 4:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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