如果在opencart 3树枝文件中不起作用

问题描述

我正在尝试将以下代码集成到opencart 3中,但是我不知道为什么我的代码id无法执行?我是twig的新手。请帮助我解决此问题。

{% 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


注意:您应该启用调试模式才能看到细枝错误,在运行代码时,您显然会收到以下错误

变量“ my_total”不存在。