【问题标题】:Magento Email Template If StatementsMagento 电子邮件模板 If 语句
【发布时间】:2011-06-15 09:45:17
【问题描述】:

Magento 电子邮件模板 If 语句在我期望的情况下未评估为 true。有人可以告诉我有什么问题吗?看看下面的代码:

{{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 语句评估相等性?

【问题讨论】:

    标签: email templates magento magento-1.4


    【解决方案1】:

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

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

    虽然我的解决方案是针对不同的问题,但该方法应该适用于此。

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

    {{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>"; }?>
    

    【讨论】:

    • {{block type='core/template' area='frontend' template='paymentstatus/orderemail.phtml' order=$order}}&lt;br /&gt; 你放置的这段代码在哪里
    • 谢谢!新增俄语说明,magefast.com/letter-template-magento-fantastico
    • 我使用以下代码检索支付类型:$payment_type = $this->getData('order')->getPayment()->getMethodInstance()->getCode();跨度>
    • 您好 deanpodgornik,您在哪里添加了该代码?发送电子邮件模板或发送到 orderemail.phtml
    • 这在 1.9 中有效,但它破坏了管理中的预览功能 - 不错的权衡。
    【解决方案2】:

    挖掘代码,看起来模板逻辑是由filter函数中的Varien_Filter_Template(在lib\Varien而不是app\code下)实现的,如果模式匹配,它会向ifDirective函数发出回调正则表达式。 ifDirective 反过来使用 _getVariable 函数来评估您的 if 条件。 _getVariable 然后将Varien_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, 京东

    【讨论】:

    • 如果我错了,请纠正我,但根据你的说法,他也可以创建某种方法(也许是辅助方法?)也可以检查这个,对吧?
    • 您还可以在模板外部构建逻辑以在运行时填充某个客户变量。
    • @Joseph - 是的,您可以创建一个方法,但我认为它需要在 Customer 对象上,以便您可以使用 {{if customer.isGroupMember()}} 这需要您扩展客户模型。对于这个要求,我不一定会推荐它,但仍然是一个有效的选择
    【解决方案3】:

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

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

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

    【讨论】:

    • 是否可以检测免费送货方式? {{depend $order.freeshippingmethod}} ...感谢帮助
    【解决方案4】:

    在正常的 Magento 块/类中,您将使用 $customer-&gt;getGroupId() 访问组 id 值。等效的 CMS/电子邮件模板是 customer.getGroupId(),而不是您写的 customer.group_id

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 2015-08-06
      相关资源
      最近更新 更多