21

Magento 电子邮件模板 If Statements 在我期望的时候没有被评估为真。有人可以告诉我有什么问题吗?看看下面的代码:

{{var customer.group_id}}
{{if customer.group_id}}Print true{{else}}Print false{{/if}}
{{if customer.group_id==4}}Print true{{else}}Print false{{/if}}
{{if customer.group_id=4}}Print true{{else}}Print false{{/if}}
{{if customer.group_id eq 4}}Print true{{else}}Print false{{/if}}

输出是

4
Print True
Print False
Print False
Print False

我尝试在 4 周围加上引号,但结果相同。如何使用 magento 电子邮件模板 if 语句评估相等性?

4

4 回答 4

33

我通过使用“块”技术解决了这个问题。

您所做的是将订单传递给一个块,然后在该块内执行您的逻辑。

尽管我的解决方案是针对不同的问题,但该方法应该在这里起作用。

我想要的是通过支票付款选项和确认电子邮件中的一些额外文字提醒他们付款。我将此添加到新的订单模板中:

{{block type='core/template' area='frontend' template='paymentstatus/orderemail.phtml' order=$order}}<br />

然后我创建了一个文件app/design/frontend/default/default/template/paymentstatus/orderemail.phtml

这具有“如果”逻辑,就我而言,我想查看订单状态是否为支票状态,然后才提醒客户他们的订单需要清算资金。

<?php if($this->getData('order')->getStatus()=='cheque') {
echo "<p>Please note that we will require your cheque to clear before we can despatch your order.</p>"; }?>
于 2011-07-08T17:58:39.590 回答
16

深入研究代码,看起来模板逻辑是由函数中的Varien_Filter_Template(在 lib\Varien 而不是 app\code 下)实现的,如果模式与正则表达式匹配,filter该函数会向函数发出回调。反过来使用该ifDirective功能来评估您的状况。然后将条件标记为属性或方法。 ifDirective_getVariableif_getVariableVarien_Filter_Template_Tokenizer_Variable

if($this->isWhiteSpace()) {
            // Ignore white spaces
            continue;
        } else if($this->char()!='.' && $this->char()!='(') {
            // Property or method name
            $parameterName .= $this->char();
        } else if($this->char()=='(') {
            // Method declaration
            $methodArgs = $this->getMethodArgs();
            $actions[] = array('type'=>'method',
                               'name'=>$parameterName,
                               'args'=>$methodArgs);
            $parameterName = '';
        } else if($parameterName!='') {
            // Property or variable declaration
            if($variableSet) {
                $actions[] = array('type'=>'property',
                                   'name'=>$parameterName);
            } else {
                $variableSet = true;
                $actions[] = array('type'=>'variable',
                                   'name'=>$parameterName);
            }
            $parameterName = '';
        }

当检测到 if 条件是一个方法时,它将执行该方法,否则它只是返回变量的字符串值。

所有这些都意味着(我认为!)如果你想在 if 语句中计算一个表达式,你需要添加一个模板可以计算的新客户属性(有可用的扩展)。因此,如果您定义一个布尔“isMemberOfGroupNameX”属性,那么模板应该可以工作。

我想这不是您要寻找的答案,但我很确定情况确实如此。

HTH, 法学博士

于 2011-01-25T23:59:45.097 回答
7

我能够或多或少地使用 {{depend}} 模板标签在模板中完成这项工作。

{{depend somevar}}
Print this if somevar evaluates to true
{{/depend}}

您必须在 app/code/local/Mage/Sales/Model/Order.php 中使用 sendNewOrderEmail() 等方法中的这个变量。

于 2011-08-14T08:59:46.813 回答
0

在普通的 Magento 块/类中,您将用于$customer->getGroupId()访问组 id 值。CMS/Email 模板等价物是customer.getGroupId()customer.group_id不像你写的那样。

于 2014-09-19T19:30:12.260 回答