Meu*_*les 5 arrays saas symfony twig
我无法使用 Twig 检查数组中是否存在值。如果购物车中有某种产品,我想在结账时隐藏运输方式。
我只能使用Twig代码,所以我必须在其中找到逻辑。
假设当产品 ID 1234 在购物车中时我想隐藏#certain_div
所以我所拥有的是这个->
{% if checkout %}
{% set array = theme.sku_shipping_rule | split(',') %}
// theme.sku_shipping_rule = a text string like 1234, 4321, 5478
{% if checkout.products %}
{% for product in checkout.products %}
{% if product.sku in array %}
<style>
#certain_div {
display: none;
}
</style>
{% endif %}
{% endfor %}
{% endif %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
我面临的问题是我的代码似乎总是返回 true。因此,即使 与product.sku数组中的值不匹配,它仍然会隐藏#certain_div。我已经通过放置{{ product.sku }}在之前进行了测试<style>。
我有什么错吗?
非常感谢任何帮助!
更新:
我已经更新了问题/代码以显示发生的情况
{% if checkout %}
{% set skuToCheck = theme.sku_shipping_rule | split(',') %}
{% set skuInCart = [] %}
{% if checkout.quote.products %}
{% for product in checkout.quote.products %}
{% set skuInCart = skuInCart | merge([product.sku]) %}
{% endfor %}
{% endif %}
{% for myVar in skuInCart %}
{{ myVar }}<br/>
{% endfor %}
// this prints
PSYGA1 // where this sku should NOT match
FP32MA4
{% for myVar in skuToCheck %}
{{ myVar }}<br/>
// this prints
FP32LY4
FP32STR4
FP32MA4
{% if myVar in skuInCart %} // also tried with | keys filter
{{ myVar }} is found
{% endif %}
{% endfor %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
所以我所做的是将购物车中产品的 sku 放入一个数组中skuInCart。接下来我想检查数组myVar中是否存在skuInCart。如果是这样打印myVar is found.
发生的情况是您应该期望它只打印匹配的结果。然而,它实际上打印所有存在的值skuInCart(使用keys过滤器)或完全空白而不使用keys过滤器。
理论上你所做的应该是有效的,看看这个小提琴示例来向你展示一个有效的演示:
基本上:
<div id="certain_div">
This should not show up
</div>
{% set searchForSku = "890" %}
{% set productSkuArrayString = "1234,4567,890" %}
{% set productSkuArray = productSkuArrayString|split(',') %}
{% if searchForSku in productSkuArray %}
<style>
#certain_div {
display: none;
}
</style>
{% endif %}
<!-- New Trial -->
<div id="certain_div">
This should show up
</div>
{% set searchForSku = "891" %}
{% set productSkuArrayString = "1234,4567,890" %}
{% set productSkuArray = productSkuArrayString|split(',') %}
{% if searchForSku in productSkuArray %}
<style>
#certain_div {
display: none;
}
</style>
{% endif %}
Run Code Online (Sandbox Code Playgroud)
将导致:
<div id="certain_div">
This should not show up
</div>
<style>
#certain_div {
display: none;
}
</style>
<!-- New Trial -->
<div id="certain_div">
This should show up
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10669 次 |
| 最近记录: |