Custom liquid file transmit objects

Hello,

first of all I’m new to shopify and liquid.

I modified code in the card-product.liquid file (Dawn Theme) line 127.

…modified theme code

The modification is doing what I expect. Modifying the existing badge for “Sale” and add a aditional badge for “2.nd hand”

Now I would like to “outsource” the modified theme code in a seperate custom-liquid-file and call it with

{% render ‘custom-liquid-file’ %}

I copied everything including the div tags

…modified theme code

And pasted it in a custom liquid file (OurCompanyName-CustomBadges.liquid)

instate of the original code I inserted {% render OurCompanyName-CustomBadges %}

As expected it is not working.

I assume the objects card_product and settings don’t exist in OurCompanyName-CustomBadges.liquid

I tried {% render OurCompanyName-CustomBadges, card_product:card_product, settings:settings %}

But I have nothing written in OurCompanyName-CustomBadges.liquid to receive this object variables.
As expected it doesn’t work.

I tried {%- include card_product, settings -%} in the first line of OurCompanyName-CustomBadges.liquid without success.

I have no clue where I can find documentation on how this works and spend hours to research it.

It seems a extrem simple beginner question, so simple that nobody explained it somewhere.

I would realy appreciate if somebody could help me out on this.

The purpose of my approach here is to transfer custom code clean and easy to new versions of the theme.

Each custom code would have its on file beginning with companyname-…

And in the theme liquid files there would be only one line with {% render …}

Our custom liquid file would have at the beginning a comment section describing where the {% render …} needs to be paste after a theme update.

This way we only have to search for companyname in the current version and transfer all this files to the new version of the theme. Then copy the line in the comment section at the top inside the file and replace theme code as described also in this comment section.

You’re actually on the right track! The issue is how you’re passing and receiving variables in your custom Liquid file, try this if its gonna work

Create Your Custom Badge Partial File

File: snippets/ourcompanyname-custom-badges.liquid

Add this code inside the file:

{% comment %} Custom badge snippet for sale and second-hand {% endcomment %}

{{ badge_content }}

Modify card-product.liquid to Call the Snippet

Replace your modified code with:

{% render ‘ourcompanyname-custom-badges’, badge_position: settings.badge_position, badge_content: ‘2nd Hand’ %}

Thank you very much. I cam closer. But still not working completely.

I think card_product is still missing.

here is the preview of the result:
https://ednywn6eavzccpih-87283007820.shopifypreview.com

And here is as it looks with the modified code in the liquid file of the theme:
https://shop.planet-para.de/collections/gebraucht

Here is how I call our own liquid file:
{% render ‘planet-para-lables’, badge_position: settings.badge_position, badge_content: ‘Gebraucht’ %}

(‘Gebraucht’ is the german term for “2nd hand”)

Hier is the modified code including your suggestion at the beginning, as it is included in our custom liquid file:

{% comment %} Custom badge snippet for sale and second-hand {% endcomment %}

{{ badge_content }}

{%- if card_product.available == false -%}
<span
id=“NoMediaStandardBadge-{{ section_id }}-{{ card_product.id }}”
class=“badge badge–bottom-left color-{{ settings.sold_out_badge_color_scheme }}”

{{- ‘products.product.sold_out’ | t -}}

{%- elsif card_product.compare_at_price > card_product.price and card_product.available -%}
{% comment %}
<span
id=“NoMediaStandardBadge-{{ section_id }}-{{ card_product.id }}”
class=“badge badge–bottom-left color-{{ settings.sale_badge_color_scheme }}”

{% endcomment %}

{{- 'products.product.on_sale' | t -}}
{% comment %} {% endcomment %}

{%- endif -%}

{% comment %}

{% endcomment %}
{%- if card_product.metafields.custom.neu_o_gebraucht.value contains “Gebraucht” -%}

Gebraucht
{% else %} {% comment %} {{- card_product.metafields.custom.neu_o_gebraucht.value -}} {% endcomment %} {%- endif -%}