将产品变量 Shopify 传递给代码段

时间:2021-03-25 10:26:37

标签: shopify product render code-snippets assign

设置

我有一家以 Debut 为主题的 Shopify 商店。

在主题的 product-card-grid.liquid 文件中,我调整/添加了以下代码以在集合网格中为每个产品卡添加额外的行,

<div class="h4 grid-view-item__title product-card__title" aria-hidden="true">{{ product.title }}</div>

{% include 'product-price' incl. BTW, variant: product.selected_or_first_available_variant, product: product, show_vendor: show_vendor %}  

{%- if  product.price > 10000 and product.price < 25000 -%}        
  <font size = "2">Of renteloos 3x <strong>{{ product.price | divided_by:3 | money_without_trailing_zeros }}</strong></font>
{%- elsif  product.price > 25000 -%}
  <font size = "2">Of gespreid vanaf <strong>{{ product.price | times: 0.03287 | money_without_trailing_zeros }}</strong> p/m</font>
{%- endif -%} 

效果很好。


问题

如果我创建看起来像这样的代码段 own-collection-grid-spreadpayment.liquid

{%- if  product.price > 10000 and product.price < 25000 -%}        
    <font size = "2">Of renteloos 3x <strong>{{ product.price | divided_by:3 | money_without_trailing_zeros }}</strong></font>
{%- elsif  product.price > 25000 -%}
    <font size = "2">Of gespreid vanaf <strong>{{ product.price | times: 0.03287 | money_without_trailing_zeros }}</strong> p/m</font>
{%- endif -%}   

然后尝试像这样在 product-card-grid.liquid 中呈现代码片段,

<div class="h4 grid-view-item__title product-card__title" aria-hidden="true">{{ product.title }}</div>

{% include 'product-price' incl. BTW, variant: product.selected_or_first_available_variant, product: product, show_vendor: show_vendor %}  

{%- render 'own-collection-grid-spreadpayment' -%}        

不显示每个产品的额外行。


尝试

我知道变量应该像这样传递给一个片段,

{%- assign my_variable:'product' -%}      
{%- render 'own-collection-grid-spreadpayment', my_variable:my_variable  -%}       

但仍然没有显示每个产品的额外行。

要么是我做错了上述操作,要么是因为 product 变量本身很特殊或其他什么东西。


问题

我该怎么办,

  1. product 变量正确传递给代码片段,并且;
  2. 如何正确呈现带有 product 变量的代码段?

1 个答案:

答案 0 :(得分:1)

你的代码有一些我以前从未见过的东西是 incl. BTW

无论如何,这是shopify theme-tags

= 而非 : 分配变量, 您将 string 传递给 my_variable,而不是 object,当然它不会保存产品数据。

{% assign my_variable = product %}

{% render 'own-collection-grid-spreadpayment' with my_variable %}

{% render 'own-collection-grid-spreadpayment' with product, a: a, b: b %}

own-collection-grid-spreadpayment.liquid

{% assign product = own-collection-grid-spreadpayment %}
{%- if  product.price > 10000 and product.price < 25000 -%}        
    <font size = "2">Of renteloos 3x <strong>{{ product.price | divided_by:3 | money_without_trailing_zeros }}</strong></font>
{%- elsif  product.price > 25000 -%}
    <font size = "2">Of gespreid vanaf <strong>{{ product.price | times: 0.03287 | money_without_trailing_zeros }}</strong> p/m</font>
{%- endif -%}