在文章上显示产品

Bogdan Somlea

如何在文章页面上显示产品列表(如配方)。您能为我提供一些指导以实现该行为的方法吗?如何将这些产品动态链接到文章?在此处输入图片说明

戴夫B

一旦知道它们是什么,就可以获取它们

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

1)使用all_products [handle]

使用产品句柄从all_products全局对象获取产品

例:

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

This works well for small numbers of products, but for large numbers of products it may cause delays in page-loading times. We are also limited to only 20 (I think) calls to all_products per page, so this wouldn't work for recipes with a ton of ingredients in them.

2) Using a collection

Use a collection that contains only the products required. You can reference any arbitrary collection if you know the collection's handle. When making collections, you can also sort products manually to control the order that they appear in when you loop through it. Collections can contain an arbitrary number of products, and I believe the default pagination will give you is either the first 20 or 50 products if you don't specify any other limit. If required you can adjust the number of products served to as high as 1000 by wrapping your collection-product loop with paginate tags (though that upper limit is definitely not recommended for performance reasons)

Example:

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

The downside for both of these is that you can't write Liquid code inside your article content in Shopify, so this ingredients section would need to be written as a snippet or a section in your theme files and included in your article template used for these recipes.

This leads me to consider the next issue - you would want to include a concept of quantity with the ingredients, and so far neither of the above give us that. So now, the hard part:

Getting that information into a Liquid snippet/section in the first place

There are a few different ways that I can think of offhand that would help you out here. No one is perfect, unfortunately.

Using Metafields

Metafields are a great tool available in Shopify, but unfortunately Shopify doesn't make them easy to use [1].

Once you have a metafield-editing tool, come up with a naming structure for the 'namespace' and 'key' values. For example, you might create the following metafields for the recipe you provided. (Note: How these would be entered will depend on what metafield-editing tool you're using)

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) ...

Then, in your theme file where you are creating your ingredient list, you would have code that looks something like this:

{% 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 %}

Using Tags and Products

If you create a tag-naming scheme, you can loop through those and use them to build your ingredient list. For example, if you give the article a number of tags in the form ingredient_[product-handle]_[amount], you would be able to reference them as:

{% 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 %}

The downside to this method is that there's no easy way to reorder the tags if done this way - using a collection will give you better control of that.

Getting recipe amounts into a Collection loop

The easiest way to reference a collection would be to have a collection with the same handle as the article - then you can reference the collection and its products as:

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

This has the advantage of letting you easily sort the ingredients by setting the collection to have a Manual sorting method, but the corresponding downside is that there's no obvious place to put the quantity information.

One way to get that information in would be to use either tags or metafields - metafields would have the advantage of being able to directly access the quantity for the product - if using the naming convention above in the metafields part of this answer, you could use:

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

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

If using tags, you would need a format that could be split up like in the tag section and loop through all your tags each time to find the one for your product. If your tags were set up as ingredient_[product-handle]_[amount]

If using tags, you would need a format that could be split up like in the tag section and loop through all your tags each time to find the one for your product. If your tags were set up as the example above:

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

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

Hopefully this helps you get going!


[1] Using Metafields: There are several possible solutions for editing metafields in Shopify - my personal preference is the 'Shopify FD' extension for Chrome, but the recent updates to the Shopify admin screens are interfering with this extension's ability to load & show its metafield panels on some pages. I know that product pages still work, but some pages (like collections) don't anymore.

There are also a number of apps available for your store to edit metafields - I haven't used any of them, so I can't speak to their value, but you can view the list here: https://apps.shopify.com/search?q=metafield&st_source=

如果您有编码背景,则还可以通过将正确的数据发送到Shopify的Admin API来自己创建和更新元字段-如果您希望查看https://help.shopify.com/en/api/reference/metafield上的文档,尝试自己做。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在主页上显示文章图片和标题

在产品灯箱上显示完整的产品说明

仅显示WooCommerce管理产品列表上登录作者的产品

在magento网站上的“缺货”产品上显示产品标签

Whmcs在homepage.tpl上显示产品

在magento 1.9的主页上不显示产品

如何在已发表的文章上使用Facebook“显示有关此文章的更多信息”?

当屏幕上显示文章时,如何向div添加类?

在前端Joomla 2.5上显示文章ID +页面itemid:

Wagtail通过流域在主页上显示最新博客文章

动态博客文章上的语法突出显示不起作用

在wordpress上的帖子中显示最新文章

如何在Wordpress博客文章上显示作者?

Joomla文章和模块未在小屏幕上显示

PHP - Wordpress 上一篇文章链接显示在最后一篇文章中

显示类似文章的链接

当我在Twitter或Linkedin上共享Wordpress上的文章时,显示ASCII码

在文章列表布局中显示的文章标签

在WooCommerce折扣产品上显示自定义价格后缀

Bigcommerce-在特定类别/产品上显示某些消息/图像

JetBrains产品在触摸栏上未显示FN键

WooCommerce-在首页上显示类别及其产品

在WooCommerce管理产品列表上显示“税收状态”

如何在我的主页上显示 Woocommerce 产品?

如何在Woocommerce特色产品上显示说明?

在yii2主页上显示prestashop产品

在目标网页上显示随机的django oscar产品

Laravel在动态导航栏上显示产品页面链接

在WooCommerce变动价格上显示特定的选定产品属性