Make some code act only on a specific collection of products and not on all products

Solved
Highlighted
New Member
4 0 0

Hi there, I'm a bit of a noob, but trying to make a edit to a piece of code that I got from a tutorial. The tutorial was to add a gift wrapping option on the cart page. I changed the code in order to make it into a personal message instead of gift wrapping (as all items come gift wrapped as standard).

This is the tutorial --> https://shopify.dev/tutorials/customize-theme-add-gift-wrap-option

However, using this code the gift wrapping product gets multiplied by the number of items in the basket, but I would like it to only multiply by the number of items from a specific collection. Is this possible? I think I need to edit something in the lines of code below, but don't know what goes in there to make it only one collection and not every item...

Here is my website URL: zwparcels.thecleankilo.co.uk

  {% assign id = linklists.personalised-message.links.first.object.variants.first.id %}

  {% assign personalised_message_in_cart = 0 %}
  {% for item in cart.items %}
    {% if item.id == id %}
      {% assign personalised_message_in_cart = item.quantity %}
    {% endif %}
  {% endfor %}
  {% assign items_in_cart = cart.item_count | minus: personalised_message_in_cart %}

 

Thanks very much in advance for your help!

Tom

0 Likes
Highlighted
Excursionist
11 3 2

This is an accepted solution.

Not an expert, but I would think you'd just need to find out if the product belonged to the right collection. You could add a loop to loop through the product's collections and an "if" statement to match the correct collection id, something like: 

 

 

{% for item in cart.items %}
    {% for collection in item.product.collections %}
      {% if collection.id == id %}
        {% assign personalised_message_in_cart = item.quantity %}
      {% endif %}
    {% endfor}%
{% endfor %}

 

 

So you'd have to hardcode in the collection id, or you could tag your products with a tag like "gift_wrapping" and then check the product for the tag instead of looking at the collection it's in:

 

 

{% if item.product.tags contains "gift_wrapping" %}

 

 

 which eliminates the for loop to loop through collections. Just an idea. I know you can make automatic collections where any product tagged with a certain tag gets put into that collection. If you're doing that and your products are already tagged, then for sure just look for that tag. If you're creating collections manually, then you can auto-tag products that are put into a certain collection using an app like Mechanic.

I found the Mechanic task that would do the auto-tagging for manual collections here: https://usemechanic.com/task/auto-tag-products-in-a-manual-collection

If you haven't used Mechanic, it's essentially an app that lets you run liquid tasks on your shop, which eliminates the need for multiple apps. So if you want to auto-tag orders or customers or products, this app can do it and a ton of other things.

0 Likes
Highlighted
New Member
4 0 0
Thanks, the tagging option sounds like a great idea.
I have tagged all of my products with 'personalised message', and tried adding in your line of code: {% if item.product.tags includes "personalised_message" %}. But I imagine I need to edit a fair amount more of the code for this to work. Thanks for your help so far - don't suppose you know the next step?
Thanks
Tom
0 Likes