Add a custom message on cart drawer based on product collection

Solved

Add a custom message on cart drawer based on product collection

azieaziz14
Tourist
6 0 2

Hi,

 

How can I add a custom text below the item in cart based on item collection

 

For example, if item is from collection A, display message X. If item from collection B, display message Y below the item as screenshot below.

 

azieaziz14_0-1746523954212.png

 

I tried to add some testing line of codes below the "quantity" row, but it makes the cart broken.

azieaziz14_1-1746524060542.png

When I remove the item from the cart, it cause the cart to be broken, and I cannot make it work even after refreshing the page.

 

azieaziz14_0-1746524306891.png

 

 

Is there a way to fix this? Thanks

 

Accepted Solution (1)

Small_Task_Help
Shopify Partner
1054 45 101

This is an accepted solution.

Hi,

Hope this will help

- Find Cart Item File

{% for item in cart.items %}

Get Product Collections (Using cart.items only gives tags & product data, not collections)

Under the quantity selector add this loop

{% assign collections = item.product.collections %}
{% for collection in collections %}
  {% if collection.handle == 'collection-a' %}
    <p class="custom-cart-message">This is from Collection A – enjoy a special bonus!</p>
  {% elsif collection.handle == 'collection-b' %}
    <p class="custom-cart-message">Fast shipping on Collection B items!</p>
  {% endif %}
{% endfor %}

- Prevent Cart Breaking

To Get Shopify Experts Help, Click Here or E-mail - hi@ecommercesmalltask.com
About Us - We are Shopify Expert India
At Google My Business - Ecommerce Small Task - Hire Shopify Developers Ahmedabad

View solution in original post

Replies 3 (3)

Small_Task_Help
Shopify Partner
1054 45 101

This is an accepted solution.

Hi,

Hope this will help

- Find Cart Item File

{% for item in cart.items %}

Get Product Collections (Using cart.items only gives tags & product data, not collections)

Under the quantity selector add this loop

{% assign collections = item.product.collections %}
{% for collection in collections %}
  {% if collection.handle == 'collection-a' %}
    <p class="custom-cart-message">This is from Collection A – enjoy a special bonus!</p>
  {% elsif collection.handle == 'collection-b' %}
    <p class="custom-cart-message">Fast shipping on Collection B items!</p>
  {% endif %}
{% endfor %}

- Prevent Cart Breaking

To Get Shopify Experts Help, Click Here or E-mail - hi@ecommercesmalltask.com
About Us - We are Shopify Expert India
At Google My Business - Ecommerce Small Task - Hire Shopify Developers Ahmedabad
azieaziz14
Tourist
6 0 2

Hi,

 

I have able to make it works. I added these codes inside the cart-drawer liquid below the <quantity-popover> element.

azieaziz14_0-1746586663431.png

Result on cart

 

azieaziz14_1-1746586699440.png

In order to prevent the cart broken, I did the changes directly from the code editor in Shopify admin, instead of from the codes on my local. 

 

Thanks for the help. really apprecite it. 

 

Shadab_dev
Shopify Partner
1417 72 153

I think the reason it's broken is because you are adding extra code in there but when you do any action the cart a new cart html is sent from the server or Shopify db that does not have that code. Not sure just assuming this.

 

May be try adding the content with javascript. So when the new cart is returned, loop over the new cart and add respective messages under each item so basically creating a new cart html and i setting into the dom. 

 

I am honestly not sure how feasible this is but or if even I am right on the cause of the error in the first place but this is all just something top of the head solution. 

 

Try this and let me know.

 

Best

Shadab

Buy me Coffee, if you feel i was helpful. Email Me here or WhatsApp me with this link for any help with shopify theme customizations or any project in web dev. If this is helpful, please Like and Accept the solution.