Twig 检查值是否在数组中
Twig check if value is in array
我无法使用 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 %}
我面临的问题是我的代码似乎总是 returns 正确。因此,即使 product.sku
与数组中的值不匹配,它仍会隐藏 #certain_div
。我已经通过将 {{ product.sku }}
放在 <style>
之前进行了测试。
我做错了什么?
非常感谢任何帮助!
更新:
我更新了 question/code 以显示正在发生的事情
{% 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 %}
所以我所做的是将购物车中产品的 sku 放在一个数组中 skuInCart
。接下来我想检查 myVar
是否存在于 skuInCart
数组中。如果是这样打印 myVar is found
。
您应该期望它只打印匹配的结果。然而,它实际上打印所有存在的值 skuInCart
(使用 keys
过滤器)或完全空白而不使用 keys
过滤器。
你在理论上所做的应该有效,看看这个 fiddle 示例向你展示一个工作演示:
基本上:
<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 %}
将导致:
<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>
您可以使用iterable
来检查变量是数组还是可遍历对象:
{% if items is iterable %}
{# stuff #}
{% endif %}
我无法使用 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 %}
我面临的问题是我的代码似乎总是 returns 正确。因此,即使 product.sku
与数组中的值不匹配,它仍会隐藏 #certain_div
。我已经通过将 {{ product.sku }}
放在 <style>
之前进行了测试。
我做错了什么?
非常感谢任何帮助!
更新:
我更新了 question/code 以显示正在发生的事情
{% 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 %}
所以我所做的是将购物车中产品的 sku 放在一个数组中 skuInCart
。接下来我想检查 myVar
是否存在于 skuInCart
数组中。如果是这样打印 myVar is found
。
您应该期望它只打印匹配的结果。然而,它实际上打印所有存在的值 skuInCart
(使用 keys
过滤器)或完全空白而不使用 keys
过滤器。
你在理论上所做的应该有效,看看这个 fiddle 示例向你展示一个工作演示:
基本上:
<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 %}
将导致:
<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>
您可以使用iterable
来检查变量是数组还是可遍历对象:
{% if items is iterable %}
{# stuff #}
{% endif %}