当给定产品有两个包含特定值的自定义字段时,我想在产品详细信息页面上显示一个div。例如,我的目录中的每个产品都有以下自定义字段
当产品的InventoryOnOrder > 0并且它的InventoryInStore =0时,我想有条件地显示div元素。任何帮助,我可以得到一些类似于下面的代码片段的工作,将非常感谢!
{{#each custom_fields}}
{{#if (toLowerCase name) '===' 'inventoryonorder'}}
{{#if value '!=' 0}}
{{#if (toLowerCase name) '===' 'inventoryinstore'}}
{{#if value '===' 0}}
<div>element A</div>
{{/if}}
{{/if}}
{{/if}}
{{/if}}
{{/each}}
提前谢谢你比我聪明:)干杯!
发布于 2022-01-07 06:41:30
最好的方法不是在#each
循环中处理逻辑和呈现组件。相反,您应该使用#each
循环来迭代custom_fields
的键,并检查当前的迭代是否有inventoryonorder
或inventoryinstore
名称,如果有,则应该使用assignVar
助手为每个变量设置变量,然后在#each
块之外处理逻辑。
我能够在我的模板环境中编写一个工作示例,下面是我的代码:
{{#each product.custom_fields}}
{{#if (toLowerCase name) "===" "inventoryonorder"}}
{{assignVar "field_inventoryOnOrder" value}}
{{/if}}
{{#if (toLowerCase name) "===" "inventoryinstore"}}
{{assignVar "field_inventoryInStore" value}}
{{/if}}
{{/each}}
{{#all (if (getVar "field_inventoryOnOrder") ">" "0") (if (getVar "field_inventoryInStore") "===" "0")}}
<div>Display custom message</div>
{{/all}}
注意,在each
循环中,我不呈现任何内容--这个循环是纯工具栏,用于设置变量以供以后进行比较。如果您还没有听说过assignVar
助手,它是非常有用的!
在each
循环处理分配车把变量的逻辑之后,我立即使用#all
助手来确保所有参数都解析为true。如果它们这样做了,则应该呈现内容。
https://stackoverflow.com/questions/70616025
复制相似问题