Adding a custom message to product line on cart page based on product collection

Hello,

I am having some trouble with getting my cart to display a message on correctly. I am trying to make it so that when a product is within the “2024 Snowboards” collection is added to the it will display a message stating “FREE SOCKS WITH THIS SNOWBOARD PURCHASE” on the product line on the cart page. I got it to appear, but it is now appearing on products that are not even in the “2024 Snowboards” Collection. When I remove the product from the “2024 Snowboards” collection, all the messaging disappears as it should. I just am confused as to how to make it not appear on all products when a product from the “2024 Snowboards” collection is in the cart but still have it show on just the products within the “2024 Snowboards” collection.

Here is an image showing the error:

Code Snippet for I am using for adding text:

{% for item in cart.items %}  
  {% assign found_collection = false %}
  {% for collection in item.product.collections %}
    {% if collection.title == '2024 Snowboards' %}
      {% assign found_collection = true %}
      {% break %}
    {% endif %}
  {% endfor %}
  {% if found_collection %}
    ### FREE SOCKS WITH THIS SNOWBOARD PURCHASE!
  {% endif %}
{% endfor %}

Full cart-item code:


  

    {% if product.image != blank %}
      
        {%- render 'image-element',
          img: product,
          alt: product.product.title,
          widths: '180, 360, 540',
          sizes: sizes,
          sizeVariable: sizeVariable,
          fallback: fallback,
        -%}
      
    {% endif %}
  

  
    

      
        {{ product.product.title }}
      
      {%- unless product.product.has_only_default_variant -%}
        

          {%- for option in product.options_with_values -%}
            
{{ option.name }}: {{ option.value }}

          {%- endfor -%}
        

      {%- endunless -%}
      {%- if product.selling_plan_allocation != empty -%}
        
          {{ product.selling_plan_allocation.selling_plan.name }}
        

      {%- endif -%}
      {%- assign property_size = product.properties | size -%}
      {% if property_size > 0 %}
        {% for p in product.properties %}
          {%- assign first_character_in_key = p.first | truncate: 1, '' -%}
          {% unless p.last == blank or first_character_in_key == '_' %}
            
              {{ p.first }}:
              {% if p.last contains '/uploads/' %}
                {{ p.last | split: '/' | last }}
              {% else %}
                {{ p.last }}
              {% endif %}
            

          {% endunless %}
        {% endfor %}
      {% endif %}

    

    
  {% for item in cart.items %}  
  {% assign found_collection = false %}
  {% for collection in item.product.collections %}
    {% if collection.title == '2024 Snowboards' %}
      {% assign found_collection = true %}
      {% break %}
    {% endif %}
  {% endfor %}
  {% if found_collection %}
    ### FREE SOCKS WITH THIS SNOWBOARD PURCHASE!
  {% endif %}
{% endfor %}
    
    
      

        

          
          
          
          
        

        
          
            {{ 'cart.general.remove' | t }}
          
        

      

      
        {% if product.original_price != product.final_price %}
          {{ 'products.general.regular_price' | t }}
          <small>
            {{ product.original_price | money }}
          </small>
          {{ 'products.general.sale_price' | t }}
          
            {{ product.final_price | money }}
          
        {% else %}
          
            {{ product.original_price | money }}
          
        {% endif %}

        {%- if product.line_level_discount_allocations != blank -%}
          {%- for discount_allocation in product.line_level_discount_allocations -%}
            <small>{{ discount_allocation.discount_application.title }} (-{{ discount_allocation.amount | money }})</small>
          {%- endfor -%}
        {%- endif -%}

        {%- if product.unit_price_measurement -%}
          {%- capture unit_price_base_unit -%}
            
              {%- if product.unit_price_measurement -%}
                {%- if product.unit_price_measurement.reference_value != 1 -%}
                  {{ product.unit_price_measurement.reference_value }}
                {%- endif -%}
                {{ product.unit_price_measurement.reference_unit }}
              {%- endif -%}
            
          {%- endcapture -%}

          
{{ product.unit_price | money }}/{{ unit_price_base_unit }}

        {%- endif -%}
      

    

  

Full main-cart code:


  {%- render 'breadcrumbs' -%}

  
  
  {%- if cart.item_count > 0 -%}
    
  {%- endif -%}

{% schema %}
{
  "name": "t:sections.main-cart.name"
}
{% endschema %}

Thank you for your time if you have any solutions!

Hi,

Try this code snippet:

{%- for collection in item.product.collections -%}
  {%- if collection.title == '2024 Snowboards' -%}
    ### FREE SOCKS WITH THIS SNOWBOARD PURCHASE!
  {%- endif -%}
{%- endfor -%}

Place the snippet in “main-cart-items.liquid”, right after this (already existing) line:

{{ item.product.title | escape }}

Cheers,

Actually, the reply I posted above is for a Dawn Theme store.

Try using this code snippet instead of what you’re currently using:

{%- for item in cart.items -%}
  {%- for collection in item.product.collections -%}
    {%- if collection.title == '2024 Snowboards' -%}
      ### FREE SOCKS WITH THIS SNOWBOARD PURCHASE!
    {%- endif -%}
  {%- endfor -%}
{%- endfor -%}

Hi JohnE10,

Thank you for this snippet. It didn’t fix the issue of having the message show up on all the products in the cart however it does still work the same as the code snippet I was using before.

Thank you,
Matt

Hey Matt,

just to double-check, did you replace all of this:

{% for item in cart.items %}  
  {% assign found_collection = false %}
  {% for collection in item.product.collections %}
    {% if collection.title == '2024 Snowboards' %}
      {% assign found_collection = true %}
      {% break %}
    {% endif %}
  {% endfor %}
  {% if found_collection %}
    ### FREE SOCKS WITH THIS SNOWBOARD PURCHASE!
  {% endif %}
{% endfor %}

with the new snippet?

This is what I get with it, but that’s using the Dawn theme.

If you can give me collaborator access, I’ll take a look at it for you?