显示Shopify中特定产品的产品价格

时间:2016-09-15 20:30:15

标签: php html shopify liquid

enter image description here

我目前正在尝试在shopify商店中显示所有商品的价格。我已经设法使用以下代码为我的第一个产品展示了这个:

<div class="grid__item">
{% for collection in collections %}



    {% for product in collection.products %}
   {% for variant in product.variants %}


{% if forloop.index <=1 %}
<div id="temp_first" style="display:none;">
<div class="style14">Style</div>
<div class="style11">(No)</div>
<div class="front"><img src="//files/front.jpg?v=1473520767" title="1 Sided Full Color" /></div>
<div class="reverse"><img src="//1/1265/7581/files/reverse.jpg?v=1473520807" title="1 Sided Only" /></div>
<div class="price"><a href="/products/leaflets?variant={{ variant.id }}">{{ variant.price | money | remove: '.00'}}</a></div>

{% elsif forloop.index <7 %}
<div class="price"><a href="/products/leaflets?variant={{ variant.id }}">{{ variant.price | money | remove: '.00'}}</a></div>

{% elsif forloop.index <10 %} 
<div class="price2"><a href="/products/leaflets?variant={{ variant.id }}">{{ variant.price | money | remove: '.00'}}</a></div>

{% elsif forloop.index <13 %} 
<div class="price3"><a href="/products/leaflets?variant={{ variant.id }}">{{ variant.price | money | remove: '.00'}}</a></div>

{% elsif forloop.index ==13 %}
<div class="price3"><a href="/products/leaflets?variant={{ variant.id }}">{{ variant.price | money | remove: '.00'}}</a></div>
</div>
{% endif %}

   {% endfor %}
{% endfor %}


{% endfor %}

现在完美无缺。我现在的问题是我想显示第二个产品的价格,我复制了上面的代码并更改了链接,以便将其链接到正确的产品页面和变体,这很好。问题是显示的价格是第一个产品,我不知道我需要在代码中更改以定位第二个产品。无论是通过它的名称还是什么,我只是不确定,因此需要一些帮助。

如果有人可以提出建议,我如何定位第二个产品,我会非常感激。

提前致谢。

1 个答案:

答案 0 :(得分:0)

您可以使用以下语法获取任意产品变量:

{app}\MyApp.exe (其中'product-handle'将替换为您想要抓取的产品的句柄。)

这可以动态完成:

{% assign custom_product = all_products['product-handle'] %}

如果你有主要产品的某些属性说明了辅助产品会是什么(例如使用app或Shopify FD浏览器扩展设置的元区域),你可以参考它:

{% assign some_handle = 'product-handle' %}
{% assign custom_product = all_products[some_handle] %}

上述任何示例中的变量custom_product都是表示此时相应产品的变量,因此我们可以完全按照与主产品相同的方式引用该产品的任何产品属性。例如:

{% assign some_handle = product.metafields.custom_namespace.related_product %}
{% assign custom_product = all_products[some_handle] %}

PS - 此外,您可以使用<h3>{{ product.title }} is {{ product.price | money }}, but {{ custom_product.title }} is {{ custom_product.price | money }}</h3> 代替链接钱并删除过滤器。

PPS - 再次查看您的原始代码,请注意您可以通过动态链接来节省一些麻烦:{{ variant.price | money_without_trailing_zeros }}

PPPS - 您编写的代码试图遍历每个集合中每个产品的每个变体。根据您的产品清单的大小,这可能不起作用 - 当您没有分页时,Shopify对收集的产品数量有一个硬性上限。根据您要完成的具体内容,您可以更好地利用Liquid模板语言,以更简洁,更易于管理的方式工作。 :)

示例 (根据原始海报的说明)

如果您要在页面背景中创建多个网格,那么这样的内容会是什么?

<a href="/products/{{ product.handle }}?variant={{ variant.id }}">

这将为您关注的每个产品生成不同的HTML网格,然后您可以使用CSS规则来适当地定位/设置它们。

这会让你更接近你所追求的目标吗?