【问题标题】:Create a simple array in liquid在液体中创建一个简单的数组
【发布时间】:2020-09-15 10:14:45
【问题描述】:

我需要按数量制作一个关联的产品数组。如何将这一衬里从 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 %}

【问题讨论】:

  • 你能澄清一下你想在这里实现什么吗?由于当前代码背后没有任何逻辑(在 Shopify 术语中),这不仅是错误的,而且没有任何意义。
  • @drip 我觉得看了代码,你就能明白我在做什么了。这是不言自明的。但是如果您仍然没有得到它,我已经添加了它

标签: php shopify liquid


【解决方案1】:

您不能像在 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: ',' %}

这就是我们捕获输出并将其拆分以创建数组的原因。

【讨论】:

  • 谢谢。知道了。液体真是一团糟
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-16
  • 1970-01-01
  • 2016-06-09
  • 1970-01-01
  • 2013-04-26
  • 2017-03-07
  • 2011-11-15
相关资源
最近更新 更多