微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决如果在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”不存在。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。