首页 > 解决方案 > 基于分组标签的相关产品会限制内存。使用带有特定句柄标签的集合

问题描述

从这一点开始https://www.shopify.com/partners/blog/related-products 我最终得到了这个自定义和工作代码:

    {% comment %}
     Get dynamic tag from product and create static collection handle 
    {% endcomment %}
        {% for tag in product.tags %}
            {% if tag contains 'IDSeries_' %}
                {% assign ser_tag = tag %}
                    <li>
                        <a href="/collections/all/{{ ser_tag | handleize }}">
                            <h2>{{ ser_tag | remove: "IDSeries_" | prepend: "Go to products with Serie ID number:" }}</h2>
                        </a>
                    </li>
            {% endif %}
        {% endfor %}

    {% comment %} 
     Return products by using the extracted tag 
    {% endcomment %}
        {% for product in collections.all.products %}
            {% if product.tags contains ser_tag %}
                {{ product.title }}
            {% endif %}
        {% endfor %}

上面这个确实有效。但这不是解决方案!
由于内存有限,它仅适用于第一批产品,而不适用于所有产品。

所以在 Themes/Debut/Sections/product-template.liquid
我尝试以下没有运气

{% assign collection_handle = ser_tag %}
    {% for product in collections.all[collection_handle].products %}
        {{ product.title }}
    {% endfor %}

问题:
关于如何为如此大量的产品实现一致的结果的任何线索?

额外的问题:



我还创建了一个 Shopify 社区主题https://community.shopify.com/c/Technical-QA/Related-products-based-on-grouping-tags-limit-memory-Use-instead/td-p/759705

标签: shopify

解决方案


我可以为您的问题想出两种解决方案,这不会影响网站的速度。

基于搜索 + Javascript

Shopify 上的搜索页面也可以搜索标签。

因此,如果您向搜索页面发出 fetch 请求,如下所示:

fetch('/search?q=IDSeries_*').then(res => res).then(res => console.log(res))

这将返回包含此标签组合的产品,您可以使用 javascript 附加它。

因此,您将使用 javascript 填充产品。

GraphQL

您可以使用 Storefront GraphQL API 发出请求并按特定标签获取产品,如下所示:

{
    products(first: 10, query:"tag:>'IDSeries_'"){
    edges {
      cursor
      node {
        title
      }
    }
  }
}

这需要了解 GraphQL,尤其是他们的 Store-Front API(不是 Admin API)。


对于一家拥有 30k 个产品的商店,每个产品有 5 个标签(总共 150k 个标签)。

标签有限制吗?

集合的all_tags对象最多可以返回 1000 个标签,其余的将被跳过。它们需要是唯一的标签,可重复的标签不算数。

除此之外,我的知识没有限制。


推荐阅读