在文章中显示产品

Show products on article

如何在文章页面上显示产品列表(如食谱)。 你能给我一些关于我应该用什么来实现这种行为的指导吗? 我如何 link 将那些产品动态添加到文章中?

这是一个一定会成功的秘诀。写你的文章。它会有一个独特的手柄。将其保存在您的头脑、剪贴板等中。现在创建一个手册 collection。给它相同的句柄。现在您可以通过 Liquid:

引用一个空的 collection
collection['some-handle-to-an-article']

如果您现在用食谱中的产品填充 collection 会怎么样?尤里卡。亲切!然后,您可以将它们列在一个简单的 Liquid for next 循环中,如下所示:

{% for product in collection['some-handle-to-an-article'] %}
  {{ product.title }}
{% endfor %}

或者你也可以做个机灵鬼,自己动手收集产品的把手。将它们存储在分配给文章的元字段资源中。例如,像 'a-blah,b-blahblah,c-zigo-von-goober' 这样的字符串,然后使用 Liquid 在文章模板中查找该元字段。用逗号分隔该字符串。现在用最优秀的all_products像这样:

{% assign fizzbuzz = all_products['a-blah'] %}
  {{ fizzbuzz.title }}

还有更多创意选择。 Shopify Liquid 适合这样玩。没有限制,除了 all_products 在 20...你不能超过那个。

一旦我们知道它们是什么就得到产品

有两种方法可以在 Shopify 的任意页面上显示产品:

1) 使用 all_products[句柄]

使用产品句柄从 all_products 全局 object 获取产品。

示例:

{% assign ingredient = all_products['lavender-oil'] %}

这适用于少量产品,但对于大量产品,它可能会导致延迟 page-loading 次。我们也被限制为每页只能调用 20 次(我认为)all_products,因此这不适用于包含大量成分的食谱。

2) 使用 collection

使用仅包含所需产品的 collection。如果您知道 collection 的句柄,则可以引用任意 collection。在制作 collection 时,您还可以手动对产品进行排序,以控制它们在循环时出现的顺序。 Collections 可以包含任意数量的产品,我相信如果您没有指定任何其他限制,默认分页将为您提供前 20 或 50 个产品。如果需要,您可以通过使用 paginate 标签包装 collection-product 循环来将提供的产品数量调整为高达 1000(尽管出于性能原因绝对不建议使用上限)

示例:

{% assign ingredients = collections['love-potion-number-9'] %}
{% for product in ingredients.products %}
  <h2>{{ product.name }}!!</h2>
{% endfor %}

这两种方法的缺点是您不能在 Shopify 的文章内容中编写 Liquid 代码,因此该配料部分需要作为片段或主题文件中的一个部分编写并包含在您的用于这些食谱的文章模板。

这让我考虑下一个问题 - 您可能希望在配料中加入数量概念,但到目前为止,以上都没有给我们这个概念。那么现在,困难的部分:

首先将该信息放入 Liquid snippet/section

我可以立即想到几种不同的方法来帮助您。不幸的是,没有人是完美的。

使用元字段

Metafields 是 Shopify 中可用的一个很好的工具,但遗憾的是 Shopify 并没有使它们易于使用 [1]。

有了 metafield-editing 工具后,为 'namespace' 和 'key' 值想出一个命名结构。例如,您可以为您提供的食谱创建以下元字段。 (注意:如何输入这些内容取决于您使用的 metafield-editing 工具)

namespace: 'ingredients',  // We'll use this as the 'box' that holds all our ingredients
key: 'juniper-berry-oil', // This will be the product handle for the product in question
value: '2 drops' // The quantity used for the recipe

namespace: 'ingredients',
key: 'rosemary-ct-camphor-oil',
value: '1 drop'

namespace: 'ingredients',
key: 'cypress-oil',
value: '1 drop'

... (etc) ...

然后,在您创建配料表的主题文件中,您将拥有如下所示的代码:

{% assign ingredients = article.metafields.ingredients %}
{% for ingredient in ingredients %}
  {% assign handle = ingredient.first %}
  {% assign amount = ingredient.last %}
  {% assign product = all_products[handle] %}

  <!-- HTML code here -->

{% endfor %}

使用标签和产品

如果您创建一个 tag-naming 方案,您可以遍历这些方案并使用它们来构建您的配料表。例如,如果您以 ingredient_[product-handle]_[amount] 的形式为文章提供多个标签,您可以将它们引用为:

{% for tag in article.tags %}
  {% if tag contains 'ingredient' %}
     {% assign breakdown = tag | split: '_' %}

     {% assign handle = breakdown[1] %}
     {% assign product = all_products[handle] %}

     {% assign amount = breakdown | last %}

     <!-- HTML Code -->
  {% endif %}
{% endfor %}

此方法的缺点是,如果以这种方式重新排序标签,则没有简单的方法 - 使用 collection 可以更好地控制它。

将配方量放入 Collection 循环中

引用 collection 的最简单方法是使用与文章相同句柄的 collection - 然后您可以引用 collection 及其产品:

{% assign ingredients = collections[article.handle] %}
{% for product in ingredients.products %}
   <!-- HTML Code here -->
{% endfor %}

这样的好处是可以通过设置collection手动排序方式让你轻松的对食材进行排序,但是相应的缺点是没有明显的地方可以放置数量信息。

获取该信息的一种方法是使用标签或元字段 - 元字段的优势在于能够直接访问产品的数量 - 如果在元字段部分使用上面的命名约定回答,你可以使用:

{% assign ingredients = collections[article.handle] %}
{% for product in ingredients.products %}
  {% assign amount = article.metafields.ingredients[product.handle] %}

  <!-- HTML Code here -->
{% endfor %}

如果使用标签,您将需要一种可以像标签部分那样拆分的格式,并每次循环遍历所有标签以找到适合您产品的标签。如果您的标签设置为 ingredient_[product-handle]_[amount]

如果使用标签,您将需要一种可以像标签部分那样拆分的格式,并每次循环遍历所有标签以找到适合您产品的标签。如果您的标签设置如上例:

{% for tag in article.tags %}
  {% if tag contains 'ingredient' and tag contains product.handle %}
     {% assign amount = tag | split: '_' | last %}
  {% endif %}

    <!-- HTML Code -->
{% endfor %}

希望这对您有所帮助!


[1] 使用元字段: 在 Shopify 中编辑元字段有几种可能的解决方案 - 我个人的偏好是 'Shopify FD' Chrome 的扩展名,但是最近对 Shopify 管理屏幕的更新干扰了此扩展程序在某些页面上加载和显示其元字段面板的能力。我知道产品页面仍然有效,但有些页面(如 collections)不再有效。

您的商店还有许多应用程序可用于编辑元字段 - 我没有使用过其中任何一个,所以我不能说出它们的价值,但您可以在此处查看列表:https://apps.shopify.com/search?q=metafield&st_source=

如果您有编码背景,您还可以通过将正确的数据发送到 Shopif 来自行创建和更新元字段的管理员 API - 如果您想自己尝试,请参阅 https://help.shopify.com/en/api/reference/metafield 处的文档。