2

I am trying to dynamically assign a template based on a condition. My helper works when called one way, but not another, which makes me believe I am using layout XML incorrectly.

To clarify, my helper is working correctly, but I am having trouble calling the helper in this case.

This works

Calling getOrderEmailItemsTemplate()

<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="Emailbygroup/Data/getOrderEmailItemsTemplate"></template>
    </action>
...

This doesn't work

Calling getOrderEmailTotalsTemplate()

...
<block type="core/template" name="order_totals_wrapper" as="order_totals" template="email/order/totals/wrapper.phtml">
    <block type="sales/order_totals" name="order_totals" helper="Emailbygroup/Data/getOrderEmailTotalsTemplate">
...

Helper

The helper just checks a condition and returns the template path based on that. I've added a log command to test if the Helper is even being loaded, but it isn't.

Mage::log("getOrderEmailTotalsTemplate is loaded... distributor ".$isDistributor,null,"tax.log");
if($isDistributor) {
    return 'sales/order/totals-discount.phtml';
} else {
    return 'sales/order/totals.phtml';
}

3 Answers 3

4

xml file

<block type="core/template" name="order_totals_wrapper" as="order_totals" template="email/order/totals/wrapper.phtml">
    <block type="sales/order_totals" name="order_totals">

Extend sales/order_totals block and add the following

protected function _construct()
{
    parent::_construct();
    $this->setTemplate(Mage::helper('Emailbygroup')->getOrderEmailTotalsTemplate());
} 

The other way to do this without extending the block is on the xml file,

<block type="core/template" name="order_totals_wrapper" as="order_totals" template="email/order/totals/wrapper.phtml">
    <block type="sales/order_totals" name="order_totals">
        <action method="setTemplate"><template helper="Emailbygroup/Data/getOrderEmailTotalsTemplate"></template></action>
5
  • So I'll add a 2nd class to my Helper file (which I'll need to define in my module's config.xml) and then add the information you've suggested. Does that sound about right?
    – travisw
    Commented Jul 15, 2015 at 16:36
  • No. Your helper file works fine. Problem is just calling the template. You need to use setTemplate in your block to set the correct phtml file. If sales/order_totals is a core file, then you need to extend it to avoid writing core files. The other way you can try is on your xml file
    – eetzen
    Commented Jul 15, 2015 at 16:53
  • Ok, it seems like I got things working. Easier than I expected, though, so I'm wary. I'll add all the changes as a separate answer for future visitors. Thanks for your help!
    – travisw
    Commented Jul 15, 2015 at 17:01
  • Or you can try out the xml file I just Edited on the Answer I posted.
    – eetzen
    Commented Jul 15, 2015 at 17:03
  • Both ways work, but using setTemplate is probably the better of the two as far as being most compatible with other modules, right?
    – travisw
    Commented Jul 15, 2015 at 17:11
1

Thanks to help from @eetzen I was able to get things working. Here are a couple of links that helped me and the changes I had to make to get the first solution working.

Note: The better way is to use @eetzen's 2nd suggestion to use the setTemplate method.

Extending Core Classes

Overriding Magento Blocks

Block Rewrite

This will actually call your helper method.

class Namespace_Modulename_Block_Order_Totals extends Mage_Sales_Block_Order_Totals {
    protected function _construct() {
        parent::_construct();
        $this->setTemplate(Mage::helper('Modulename')->methodName());
    }
}

XML

Then, you need to add your block rewrite in your module's config.xml file.

<global>
    <blocks>
        <sales>
            <rewrite>
                <order_totals>Namespace_Modulename_Block_Order_Totals</order_totals>
            </rewrite>
        </sales>
    </blocks>
...
0

adding an attribute to the block element will add it to the initial data like this:

'template' => 'order.phtml'

You changed this to:

'helper' => 'your/helper/method'

Which doesn't make sense. AFAIK there is no way to use a helper for constructor arguments like for actions. But the good news is, you don't have to. Just use the same approach as for the other block with the setTemplate action. It's not required at all to pass the template to the block constructor, just convenient in most cases

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.