Trying to get this logic: IF cart item collection title = food [do this] else, [do this]

Hi all,

As the title suggests, I’m trying to get an if/else logic to work and have tried many different ways. The snippet below has been the most “successful” in terms of actually getting an output:

{% if line_item.product.collections == ‘FOOD’ %}
{% assign group = “FOOD” %}
{% else %}
{% assign group = “OTHER” %}
{% endif %}

and also tried:

{% if line_item.product.collections contains “FOOD” %}

{% assign group = “FOOD” %}

{% else %}

{% assign group = “OTHER” %}

{% endif %}

However, with both snippets I only see “OTHER” even though I purchased an item that is in a collection named FOOD.

I hope this is clear but I’m happy to elaborate if/where necessary.

Thanks in advance!

Hi @Brkn

Try this code as written here: https://shopify.dev/docs/themes/liquid/reference/objects/product#product-collections

{% for collection in line_item.product.collections %}
    {% if collection.title == "FOOD"%}
        {% assign group = "FOOD" %}
    {% else %} 
        {% assign group = "OTHER" %}
    {% endif %}
{% endfor %}

Hope that helps you.

1 Like

Hi there,

Thanks for your input!

Unfortunately, no luck. I am trying this in the Checkout>>Additional Scripts box.

From research, I’ve seen that collection variables do not work at checkout meaning that although the scripts we’re trying are correct, it’s not accepted at checkout.

Is there an alternative method of doing this?

Thanks in advance!

Hey @Brkn

Only Shopify Plus customers have the possibility to change the “Checkout” page.

You can check here: https://shopify.dev/docs/themes/theme-templates/checkout-liquid

1 Like