我需要按產品的數量制作一個關聯數組。如何將這一襯墊從 php 轉換為液體foreach(items as item) $product[$item.product_id] += $item.quantity我想到的最好的就是這個{% for item in cart.items %} {% assign product[item.product_id] = 0 | plus product[item.product_id] | plus item.quantity %}{% endfor %}
1 回答

慕虎7371278
TA貢獻1802條經驗 獲得超4個贊
你不能像在 Liquid 中的其他語言一樣創建數組。創建數組的唯一方法是在拆分字符串之后。
所以你不能使用創建數組項product[item.product_id]。
您首先需要生成一個字符串,然后通過拆分該字符串來創建數組。
{%- capture items -%}
{%- for line_item in cart.items -%}
{{- line_item.product_id -}}|{{-line_item.quantity-}},
{%- endfor -%}
{%- endcapture -%}
{% assign items_array = items | split: ',' %}
這就是我們捕獲輸出并將其分割以創建數組的原因。
- 1 回答
- 0 關注
- 154 瀏覽
添加回答
舉報
0/150
提交
取消