否则不能在 opencart 3 twig 文件中工作

if else not working in opencart 3 twig file

我正在尝试将以下代码集成到 opencart 3 中,但我不知道为什么我的代码 id 没有执行?我是新手 twig.Please 帮我解决这个问题。

{% for product in products %}
 {{ product.total}}  
 {% set my_total = product.total|replace({'AED': '', ',': ''}) %}
 {{ my_total }}
  {% endfor %}
{% if my_total >= 500.00 %}
{% set randomPassword = [] %}
{% set alpha = 'abcdefghijklmnopqrstuvwxyz' %}
{% set numbers = '0123456789' %}
{% for i in 1..10 %}
{% set randomCharacter = random(alpha ~ alpha|upper ~ numbers ~ '-_') %}
{% set randomPassword = randomPassword|merge([randomCharacter]) %}
{% endfor %}
{% set randomPassword = randomPassword|join %}
{{ order_id }}{{ randomPassword }}
{% else %}
(text)
{% endif %}

for 循环内定义的变量仅在此类循环内已知。 当您尝试在循环外访问 my_total 时,您需要在循环外定义它,例如

{% set my_total = 0 %} {# <--- outside the loop #}

{% for product in products %}
    {% set my_total = my_total + product.total|replace({'AED': '', ',': ''}) %}
{% endfor %}

{% if my_total >= 500.00 %} {# <--- still accessible #}

    {% set randomPassword = [] %}
    {% set alpha = 'abcdefghijklmnopqrstuvwxyz' %}
    {% set numbers = '0123456789' %}

    {% for i in 1..10 %}
        {% set randomCharacter = random(alpha ~ alpha|upper ~ numbers ~ '-_') %}
        {% set randomPassword = randomPassword|merge([randomCharacter]) %}
    {% endfor %}

    {% set randomPassword = randomPassword|join %}
    {{ order_id }}{{ randomPassword }}
{% else %}
    (text)
{% endif %}

demo


注意:您应该启用调试模式以查看 twig 错误,当 运行 您的代码明显出现以下错误时

Variable "my_total" does not exist.