将字符串放入阵列Shopify Liquid

发布于 2025-01-31 11:26:18 字数 205 浏览 3 评论 0原文

在Shopify中,有具有多个数字的元场产品。这些数字代表了产品内鱼类的捕鱼区。 例如,对于产品A,值只是“ 27”,对于产品B,值为“ 27/48”。我要走的方法是创建一个数组,并在此数组中放入值。 这样的事情: 如果Metafield包含27个,则将“大西洋”放在阵列内。如果Metafield包含48个,则将“太平洋”放在阵列内。最后,我会打印出数组。 这是一个好方法还是更好的方法? 谢谢

In shopify there are products with metafields, which contain multiple numbers. These numbers represent the fishing zones of the fish that is inside the product.
For example, for product A the value is simply '27' and for product B the value is '27/48'. My way to go would be to create an array and put in values inside of this array.
Something like this:
If the metafield contains 27, then put 'Atlantic Ocean' inside of the array. If the metafield contains 48, then put 'Pacific Ocean' inside of the array. At the end I would print out the array.
Is this a good way to go or is there a better one?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无敌元气妹 2025-02-07 11:26:18

我只是假设在Metafield中保存“大西洋”等,因为阵列不是您的选择。 这就是我要处理的方式:

所有定义的全局元场

{
  "27": "Atlantic Ocean",
  "48": "Pacific Ocean"
}

因此,

{%- liquid
  assign definitions = shop.metafields.namespace.key.value
  assign parts = product.metafields.namespace_key.key.value | split: '/'

  for part in parts
    echo definitions[part]

    unless forloop.index == parts.size
      echo ', '
    endunless
  endfor
-%}

创建

Atlantic Ocean

一个包含

Atlantic Ocean, Pacific Ocean

。一种新型的海洋,无需更新液体源代码。

I'm just assuming that saving "Atlantic Ocean" etc. in the Metafield as an array is not an option for you. So here's how I would approach this:

Create a global metafield that contains all definitions like so:

{
  "27": "Atlantic Ocean",
  "48": "Pacific Ocean"
}

Then, in liquid:

{%- liquid
  assign definitions = shop.metafields.namespace.key.value
  assign parts = product.metafields.namespace_key.key.value | split: '/'

  for part in parts
    echo definitions[part]

    unless forloop.index == parts.size
      echo ', '
    endunless
  endfor
-%}

This would render for product A:

Atlantic Ocean

And for product B:

Atlantic Ocean, Pacific Ocean

This way you can easily update the definition JSON when you want to add a new type of ocean without updating the liquid source code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文