簡體   English   中英

Twig電子郵件 - 顯示和隱藏對象

[英]Twig email - show and hide object

目前使用Twig生成我的電子郵件模板。 我在解決以下問題時遇到了一些困難。

我有2個產品被推送到我的電子郵件模板,但其中一個產品是'禮品包裝'。 我的想法不是在我的產品列表中顯示GIFT WRAP,而是在摘要區域顯示,可以找到SHIPPING,SUBTOTAL和TOTAL。

我可以從列表中隱藏GIFT WRAP,但是在摘要部分存在問題。

這是我到目前為止:

<!-- Summary section -->
{% for item in items %}
   {% if item.title == 'Gift wrap' %}                 
       Show gift wrap
   {% else %} 
       Dont show giftwrap
       But now show another element
   {% endif %}
{% endfor %}

不幸的是,“不顯示禮品包裝”仍然顯示。 如果有人能幫助我,我將非常感激。

這里要求的是我的模板的一部分

 {% for item in items %}
     {% if item.title == 'Gift wrap' %}                 
          <tr>
             <td style="border-top: 2px solid #202020;"></td>
             <td style="padding-left: 10px; border-top: 2px solid #202020; padding-top: 20px; font-family: 'Open sans', Helvetica, Arial, sans-serif; color: #666666; font-weight: 300;" width="100px">
                 Subtotal
             </td>
             <td style="border-top: 2px solid #202020; padding-top: 20px; font-family: 'Open sans', Helvetica, Arial, sans-serif; color: #666666; font-weight: 300;" align="right" width="100px">
                 £{{ subtotal - item.totals.data.rounded.with_tax }}
             </td>
         </tr>
         <tr>
             <td></td>
             <td style="padding-left: 10px; padding-top: 10px; font-family: 'Open sans', Helvetica, Arial, sans-serif; color: #666666; font-weight: 300;" width="100px">
                  Gift wrap
             </td>
             <td style="padding-top: 10px; font-family: 'Open sans', Helvetica, Arial, sans-serif; color: #666666; font-weight: 300;" align="right" width="100px">
                  {{ item.totals.data.formatted.with_tax }}
              </td>
          </tr>
     {% else %}
          <tr>
               <td>
                   <p>Add this if gift wrap has not be added to customers order</p>
               </td>
          </tr>
     {% endif %}
 {% endfor %}

我猜你要做你想做的事,你需要保持一個flagGift wrap是否在項目中。 以下是使用flag解決此問題的方法:

{% set has_gift_wrap = false %}
{% for item in items %}
    {% if item.title == 'Gift wrap' %}
        {% set has_gift_wrap = true %}
    {% endif %}
{% endfor %}
 ...
 ...
{% if has_gift_wrap %}                 
    <tr>
     <td style="border-top: 2px solid #202020;"></td>
     <td style="padding-left: 10px; border-top: 2px solid #202020; padding-top: 20px; font-family: 'Open sans', Helvetica, Arial, sans-serif; color: #666666; font-weight: 300;" width="100px">
         Subtotal
     </td>
     <td style="border-top: 2px solid #202020; padding-top: 20px; font-family: 'Open sans', Helvetica, Arial, sans-serif; color: #666666; font-weight: 300;" align="right" width="100px">
         £{{ subtotal - item.totals.data.rounded.with_tax }}
     </td>
    </tr>
    <tr>
     <td></td>
     <td style="padding-left: 10px; padding-top: 10px; font-family: 'Open sans', Helvetica, Arial, sans-serif; color: #666666; font-weight: 300;" width="100px">
          Gift wrap
     </td>
     <td style="padding-top: 10px; font-family: 'Open sans', Helvetica, Arial, sans-serif; color: #666666; font-weight: 300;" align="right" width="100px">
          {{ item.totals.data.formatted.with_tax }}
      </td>
    </tr>
{% else %}
    <tr>
       <td>
           <p>Add this if gift wrap has not be added to customers order</p>
       </td>
    </tr>
{% endif %}

旁注您不能{% if "Gift wrap" in items %}使用{% if "Gift wrap" in items %}因為Gift wrap存儲在屬性中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM