Liquid - 如何创建按元字段过滤的产品列表

发布于 2025-01-13 18:58:06 字数 2072 浏览 2 评论 0原文

我正在尝试创建一个已按各自元字段过滤的产品列表。

如果我可以将嵌套属性传递给 where 过滤器(看起来这是 不可能),这将是完美的:

{% assign products = collection.products | where: "metafields.my_fields.product_in_stock", "1" %}

但是,由于似乎不可能将嵌套属性传递到 where 过滤器,是否有其他方法来构建此过滤数组?

我尝试了创建变量的多个版本,并尝试将产品分配给该变量但尚未成功。

我们的用例是: 我们有数千种产品。任何时候都有数以千计的商品缺货。我希望搜索引擎可以对这些页面进行索引,而不必从网站中删除这些页面,并且不会有数千种缺货产品堵塞我们的收藏。

总之:我希望能够显示一些缺货的产品,但不是全部。

任何帮助将不胜感激!

为什么不使用以下代码:

{% for product in products %}
  {% if product.metafields.my_fields.product_in_stock == 1 %}
    <do processing>
  {% else %}
    {% continue %}
  {% endif %}
{% endfor %}

因为按照此处。因此,如果 50 个循环中只有 46 个通过 if 条件,则页面上将仅显示 46 个产品(因为循环一旦达到 50 次迭代就会停止执行)。我想控制页面上的项目数量 - 所以这种方法不可行。

也就是说,您可以通过将分页设置为更高的值来克服 50 次迭代限制。 Shopify 文档指出分页对象限制为 50 个,但这是不正确的(截至 2022 年 3 月 17 日),因为此限制实际上是 1000 个(请参阅有关此的 SO 讨论 此处);然而这种方法引入了另外两个问题:

  1. 它是不受支持的功能,因此不清楚何时/是否会改变(Shopify 文档 指定分页应限制为 50)
  2. 分页对象现在将使用 1000,因此需要通过单独的控制机制处理结果分页。

此方法的示例如下:

{% paginate collection.products by 1000 %}
  {% assign counter = 0 %}
  {% for product in collection.products %}
    {% if counter < 50 AND product.metafields.my_fields.product_in_stock == 1 %}
      {% assign counter = counter + 1 %}
      <do processing>
    {% else %}
      {% continue %}
    {% endif %}
  {% endfor %}
{% endpaginate %}

上面的代码将允许您始终每页显示 50 个产品,但是,如果您使用与分页对象相关的任何内容 - 您将访问错误的结果 - 因为分页对象将分页 1000,当每页仅显示 50 个结果。

因此,我想知道是否有一种方法可以在液体中实现概述的功能(无需无头!如果我无头,有一百万种方法可以实现我上面概述的功能)。

具体来说:如何可靠地显示与元字段标准匹配的 50 个产品的列表?

I am trying to create a list of products that have been filtered by their respective metafields.

If I could pass nested properties to the where filter (it appears this is not possible), this would be perfect:

{% assign products = collection.products | where: "metafields.my_fields.product_in_stock", "1" %}

However, as it does not seem possible to pass nested properties to the where filter, is there an alternative approach to building this filtered array?

I have tried multiple version of creating a variable, and trying to assign products to that variable to no success yet.

Our use case is:
We have thousands of products. Thousands of them are out of stock at any one time. I would like to have the pages indexable by search engines, without having to remove the pages from the site, and without thousands of out of stock products clogging up our collections.

In summary: I would like to be able to display some out of stock products, but not all of them.

Any help would be hugely appreciated!

Why not use the following code:

{% for product in products %}
  {% if product.metafields.my_fields.product_in_stock == 1 %}
    <do processing>
  {% else %}
    {% continue %}
  {% endif %}
{% endfor %}

Because looping is limited to 50 per page as specified here. Thus, if only 46 of the 50 loops pass the if condition, only 46 products will be displayed on the page (as the loop will stop executing once it gets to 50 iterations). I would like to have control over the number of items on the page - so this approach is not feasible.

That said, you can overcome the 50 iteration limit by setting pagination to a higher value. The Shopify documentation states that the pagination object is limited to 50 but this is not correct (as of 17th March 2022) as this limit is actually 1000 (see SO discussion about this here); however this approach introduces two other problems:

  1. It is unsupported functionality so it is unclear when/if this would ever change (Shopify documentation specifies that pagination should be limited to 50)
  2. The pagination object would now be using 1000, thus paginating through results would need to be handled via a separate control mechanism.

Example of this approach here:

{% paginate collection.products by 1000 %}
  {% assign counter = 0 %}
  {% for product in collection.products %}
    {% if counter < 50 AND product.metafields.my_fields.product_in_stock == 1 %}
      {% assign counter = counter + 1 %}
      <do processing>
    {% else %}
      {% continue %}
    {% endif %}
  {% endfor %}
{% endpaginate %}

The above code will allow you to always display 50 products per page, however, if you use anything related to the paginate object - you will access the wrong results - as the paginate object will be paginating by 1000, when you are only displaying 50 results per page.

Thus, I would like to find out if there is a way to achieve the outlined functionality in liquid (without going headless! If I go headless there are a million ways I can achieve the functionality I outline above).

Specifically: how to reliably display a list of 50 products that match a metafield critera?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文