Pregunta

Estoy intentando utilizar if anidadas en mi plantilla de correo electrónico, como las siguientes:

{{if subscriber.promo_group}}
    <p>You are one of the first {{var subscriber.promo_group}} subscribers.</p>
{{/if}}

{{if subscriber.coupon_code}}
    <p>Use code {{htmlescape var=$subscriber.coupon_code}} for {{htmlescape var=$subscriber.discount_amount}} off.</p>
    {{if subscriber.partner_coupon_code}}
        <p>Or, code {{var subscriber.partner_coupon_code}} for {{htmlescape var=$subscriber.partner_discount_amount}} off at checkout.</p>
    {{/if}}
{{else}}
    {{if subscriber.partner_coupon_code}}
        <p>Use code {{htmlescape var=$subscriber.partner_coupon_code}} for {{htmlescape var=$subscriber.partner_discount_amount}} off at checkout.</p>
    {{/if}}
{{/if}}

Cuando llego a mi correo electrónico, sin embargo, lo entiendo como esto:

You are one of the first 20 subscribers.

Use code XXXXX-QTEALK15 for $35 off.

Or, code XXXXX10OFF for 10% off at checkout.

{{else}}
Use code XXXXX10OFF for 10% off at checkout.

{{/if}}

¿Es posible utilizar if anidadas en las plantillas de correo electrónico Magento?

¿Fue útil?

Solución

If you look at the start of the Varien_Filter_Template class you will find the following two constants.

const CONSTRUCTION_DEPEND_PATTERN = '/{{depend\s*(.*?)}}(.*?){{\\/depend\s*}}/si';
const CONSTRUCTION_IF_PATTERN = '/{{if\s*(.*?)}}(.*?)({{else}}(.*?))?{{\\/if\s*}}/si';

In the regular expression of CONSTRUCTION_IF_PATTERN you will notice that it has the form of

{{if condition}} TEXT GOES HERE {{else}} OTHER TEXT GOES HERE {{/if}}

So, unfortunately nesting if statements is not possible as the first matched {{/if}} will be caught in the regular expression.

Although, the class offers something other than the {{if}} statements, the {{depend}} statement. It is almost the same as the {{if}} except it has no {{else}} functionality.

Luckily in your case, the nested conditions aren't complicated and can be done using {{depend}}. So you can have the following:

{{if subscriber.promo_group}}
    <p>You are one of the first {{var subscriber.promo_group}} subscribers.</p>
{{/if}}

{{if subscriber.coupon_code}}
    <p>Use code {{htmlescape var=$subscriber.coupon_code}} for {{htmlescape var=$subscriber.discount_amount}} off.</p>
    {{depend subscriber.partner_coupon_code}}
        <p>Or, code {{var subscriber.partner_coupon_code}} for {{htmlescape var=$subscriber.partner_discount_amount}} off at checkout.</p>
    {{/depend}}
{{else}}
    {{depend subscriber.partner_coupon_code}}
        <p>Use code {{htmlescape var=$subscriber.partner_coupon_code}} for {{htmlescape var=$subscriber.partner_discount_amount}} off at checkout.</p>
    {{/depend}}
{{/if}}

If it needs to be more complicated that this, it is best to just simplify your logic using a block class for the template.

Otros consejos

I have never tried nesting control statements like that, but one possible solution (though maybe a little more work than you desire) is to include a standard .phtml template inside your email template.

The core uses this feature, and it can be very handy for situations where you need to run some PHP inside your email templates.

Have a look at:

app/locale/en_US/template/email/sales/order_new.html

as well as:

app/design/frontend/base/default/layout/sales.xml

In the order_new.html template, check line 97 (or thereabouts), you'll see this call:

{{layout handle="sales_email_order_items" order=$order}}

This call checks your layout files for the handle sales_email_order_items. In sales.xml you'll find this down near line 268. You'll see this loads in the template email/order/invoice/items.phtml (among other things).

It's all pretty standard Magento layout stuff from here. If you have a look at the items.phtml template, the first thing you'll notice is that it assigns the $_order variable. This is being passed through in the email template's layout handle as order=$order. Once you get into items.phtml, they're just assigning that variable using $this->getOrder().

Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top