Add a class to div if a product is in the cart - shopify

Is it possible to see if an item is already in the cart and add a class to div on the product page?
I was hoping something like:
{% for line_item in cart.items %}
{% if line_item.title = product.title %}
{% assign incart = "in-cart" %}
{% endif %}
{% endfor %}
would do this: {% if line_item.title = product.title %} but Shopify doesn't like it. Ideas?

line_item.title is different than product.title. Because line_item.title is the variant of the product which is in the cart.
Say product title is Shirt
And the line_item.title will be Shirt-Red(red variant)
So try this line_item.product.title

Try something like this:
{% assign in_cart = false %}
{% for item in cart.items %}
{% if item.product.handle == product.handle %}
{% assign in_cart = true %}
{% endif %}
{% endfor %}
{% if in_cart == true %}
Product is already in the cart...
{% endif %}
Also note that in your if statement you should use ==, not =.

Related

Disable checkout if products do not have same tag Shopify

I have a product (tag contains 'starter-bracelet') in my store that must be sold with another linking product (tag contains 'bracelet-link'). Is there any way to block checkout if only the starter-bracelet product is in there and then allow checkout once the linked product is added?
Thanks in advance
Okay, to hide the checkout button. you need to edit the cart.liquid code for Shopify theme version 1, and respective section code for version 2 themes.
You need to add this code to the top and check for your tag
{% assign allow_checkout = true %} // By default checkout button show
{% for line_item in cart.items %} // loop over cart items
// if tag exist
{% if line_item.product.tags contains 'starter-bracelet' %}
// set it to false
{% assign allow_checkout = false %}
{% endif %}
{% endfor %}
// wrap you current checkout button inside this below if else condition
{% allow_checkout %}
{% endif %}
I have managed to sort it, just needed to work out the correct logic.
{% assign starter_bracelet = false %}
{% assign nomination_link = false %}
{% assign allow_checkout = true %}
{% for item in cart.items %}
{% if item.product.tags contains 'starter-bracelet'%}
{% assign starter_bracelet = true %}
{% endif %}
{% if item.product.tags contains 'link' %}
{% assign nomination_link = true %}
{% endif %}
{% endfor %}
{% if starter_bracelet == true and nomination_link != true %}
{% assign allow_checkout = false %}
{% render 'nomination-checkout-message' %}
{% endif %}

Checking if no compare_price exists in Shopify Liquid

I want to check if there's no compare price, and I cannot get any of the following to work in Shopify:
{% if price > compare_at_price %}
{% if compare_at_price == 0 %}
{% if compare_at_price == "" %}
I want to output some HTML when the compare_price doesn't exist.
You are missing the object here to get its attributes:
{% if product.price > product.compare_at_price %}
Do something
{% endif %}
To check if there is one:
{% if product.compare_at_price %}
Do sthg
{% endif %}
To check if there isn't one:
{% unless product.compare_at_price %}
Do sthg
{% endunless %}
Documentation:
https://shopify.dev/docs/themes/liquid/reference/objects/product

can i use in_array () in Shopify

I am new To Shopify ! i am trying to something like,i want to check that,the ID which i put on Textbox is in array or not !
here is my Product.Liquid
{% if settings.make_an_offer %}
{% for id in product.id %}
{% include 'pricewaiter' %}
{% endfor %}
{% endif %}
in above code,i need to check that settings.make_an_offer is in this array product.id or not..
make_an_offer this is an ID of my Textbox
so how can i do this?
any help please?
Place it like this :
{% for id in product.id %}
{% if settings.make_an_offer === id %}
{% include 'pricewaiter' %}
{% endif %}
{% endfor %}

How can I check for more than 1 product with same name?

I need to check if there are two or more products with the same name so I can add an if/else statement. So I'm thinking along the lines of:
{% if product.title == product.title %}
or
{% if product.title.size > 1 %}
But this doesn't seem to be right.
Thanks!
Are you looking for a specific product title, or any product title that is duplicated? If you are comparing against a given product title, perhaps try something like this:
{% assign found_duplicate = false %}
{% for p in products %}
{% if p.title == product.title %}
{% assign found_duplicate = true %}
{% endif %}
{% endfor %}
{% if found_duplicate == true %}
...
{% endif %}

Shopify: calling swatch.liquid 'colors' on collection.liquid and product-loop.liquid

I have not been able to find a fix for my issue. I want to show a simple "More Colors" option beneath the product prices.
Screenshot of Product example
I have been testing code such as:
{% if product.variants 'color' > 1 %}
Has more than one variant.
{% else %}Only one variant.
{% endif %}
But haven't had any luck. Everything either calls "Has more..." or "Only one..." regarless of the swatch count.
I am editing product-loop.liquid which is being called from collections.liquid
It has only outputed "Only one variant" or "Has more than one variant" for every product regardless of swatch/color count.
Thanks for any help..
This statement is nonsense:
{% if product.variants 'color' > 1 %}
Instead, you want to check the options set for your variants, and if you detect an option set to color, and you have more than one variant, then clearly, you are in more than one color territory.
{% assign has_more_colors = false %}
{% for option in product.options %}
{% if option contains 'color' and product.variants.length > 1 %}
{% assign has_more_colors = true %}
{% endif %}
{% endfor %}
So now you can do whatever it is you need to do... since you now know you a) have an option called colors, and b) more than one variant, hence more than one color...
The following snippet was posted to the Shopify boards. It solved this issue:
{% assign option_title = "Color" %}
{% assign option_index = "" %}
{% assign option_values = "" %}
{% for option in product.options %}
{% if option == option_title %}
{% assign option_index = forloop.index %}
{% endif %}
{% endfor %}
{% for variant in product.variants %}
{% assign variant_option_value = "" %}
{% if option_index == 1 %}
{% assign variant_option_value = variant.option1 | handleize %}
{% elsif option_index == 2 %}
{% assign variant_option_value = variant.option2 | handleize %}
{% elsif option_index == 3 %}
{% assign variant_option_value = variant.option3 | handleize %}
{% endif %}
{% assign option_values = option_values | append:"," | append:variant_option_value %}
{% endfor %}
{% assign option_values = option_values | remove_first:"," | split:"," | uniq %}
{% if option_values.size > 1 %}
MORE COLORS
{% else %}
{% endif %}