Search code examples
shopifyproductrendercode-snippetsassign

Pass product variable Shopify to snippet


Set-up

I have a Shopify shop with the Debut theme.

In the product-card-grid.liquid file of the theme, I have adjusted/added the following code to add extra lines per product card in the collection grid,

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

which works fine.


Issue

If I create the snippet own-collection-grid-spreadpayment.liquid which looks like,

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

and then try to render the snippet in the product-card-grid.liquid like so,

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

the extra lines per product don't show.


Tries

I'm aware that variables should be passed to a snippet like so,

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

but still, the extra lines per product don't show.

Either I'm doing the above wrong, or it has to do with the product variable itself being special or something.


Question

How do I,

  1. Pass the product variable correctly to the snippet, and;
  2. How do I render the snippet with the product variable correctly?

Solution

  • Your code has something I have never seen before which is incl. BTW

    regardless, this is shopify theme-tags

    assign variable with = not :, you are passing a string to my_variable, not an object of course it will not hold product data.

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

    or

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