Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hi there!
I'm wondering how I can check cart items for specific tags to display something else.
{% for item in cart.items %}
{% if item.product.tags contains 'poster' and item.product.tags contains 'frames' %}
hello
{% endif %}
{% endfor %}
My problem is that it is currently checking for "poster" and "frames" for one product but I want them to check if any item contains these tags.
If I change the code to ...
{% for item in cart.items %}
{% if item.product.tags contains 'poster' or item.product.tags contains 'frames' %}
hello
{% endif %}
{% endfor %}
I get two greetings. Two hello's.
Any ideas?
Solved! Go to the solution
This is an accepted solution.
Try this code, Your custom code only include if both tags are available in cart :
{% assign isPosterAvailable = false %} {% assign isFramesAvailable = false %}
{% for item in cart.items %} {% if item.product.tags contains 'poster' %} {% assign isPosterAvailable = true %} {% endif %} {% if item.product.tags contains 'frames' %} {% assign isFramesAvailable = true %} {% endif %} {% endfor %} {% if isPosterAvailable and isFramesAvailable %} Add your code here {% endif %}
Let me know if you need any other help! 🙂
Thank you,
Tejas
Use following code:
{% assign isTagAvailable = false %} {% for item in cart.items %} {% if item.product.tags contains 'poster' or item.product.tags contains 'frames' %} {% assign isTagAvailable = true %} {% endif %} {% endfor %} {% if isTagAvailable %} Add your code here {% endif %}
Cheers,
Tejas
Hello @Tejas_Nadpara, thank you for replying.
Apparently the code of yours did not do the trick. It's still displaying the the code regardless. I'm now previewing with only one product in cart when this should be impossible to see. Maybe I have not explained well, so I'll make it into a scenario.
In my current cart there is only one item, this item contains "poster" tag. When I view the cart I do not see the code. I remove this product from the cart now. I am then adding a product that contains the "frames" tag. I am still not able to see the code when I view the cart. I then add a product with poster tag and I when I am to view the cart I see the code.
Only product with "frames" tag = hidden
Only product with "poster" tag = hidden
Products with "poster" and "frame" tag = show
Send me both product url so, I can take a look and assist you better.
Thank you,
Tejas
This is an accepted solution.
Try this code, Your custom code only include if both tags are available in cart :
{% assign isPosterAvailable = false %} {% assign isFramesAvailable = false %}
{% for item in cart.items %} {% if item.product.tags contains 'poster' %} {% assign isPosterAvailable = true %} {% endif %} {% if item.product.tags contains 'frames' %} {% assign isFramesAvailable = true %} {% endif %} {% endfor %} {% if isPosterAvailable and isFramesAvailable %} Add your code here {% endif %}
Let me know if you need any other help! 🙂
Thank you,
Tejas
Note that product tags are available only in the cart template. The GET /cart call has no support for items[].product_tags. Instead, one must grab items[].handle (not items[].product_id or items[].product_handle as one might assume with standard object selection methods or consistent naming conventions) and pass it to the GET /products/{product-handle} call to get tags[].
Time to see if I can keep multiple parallel callbacks in order while iterating the cart and triggering a series of /cart/updates. This should also make for an amusing customer experience under certain circumstances. My apologies in advance to anyone who might be epileptic as I have potentially lost the ability to make a single refresh.
Thank you so much @Tejas_Nadpara! I spent a day trying to do this through Javascript and this code was all I needed the whole time.
Hello @Tejas_Nadpara ,
Hope you are doing well !
I need to check that particular product is in cart or not, for that I have used same code as per you given but when I add multiple products in cart at that time this code is not working, This code is worked only for single product.
Can you please help me for multiple products in cart ?
Here I am sharing my code, please review it and help me.
{% assign productID = product.id %}
{% assign productInCart = 'false' %}
{% for item in cart.items %}
{% if item.product.id == productID %}
{% assign productInCart = 'true' %}
{% else %}
{% assign productInCart = 'false' %}
{% endif %}
{% endfor %}
Remove else part from your code:
{% assign productID = product.id %}
{% assign productInCart = 'false' %}
{% for item in cart.items %}
{% if item.product.id == productID %}
{% assign productInCart = 'true' %}
{% endif %}
{% endfor %}
Let me know if it works for you.
Hi @Tejas_Nadpara ,
By this if condition will become false then productInCart variant become false.
No, it will work. make sure that you added this code before starting the original cart item forloop.
or, send me the full code of that file so I get better idea about where exactly you added that code.
Hi @Tejas_Nadpara
I am working on product detail page dawn theme and in top of the file I have mentioned this code so on whole file I can use this variable and assign class based on this variable for any section.
{% assign productID = product.id %}
{% assign productInCart = 'false' %}
{% for item in cart.items %}
{% if item.product.id == productID %}
{% assign productInCart = 'true' %}
{% else %}
{% assign productInCart = 'false' %}
{% endif %}
{% endfor %}
--------------From here default product detail page code this is working fine when single product in cart but when multiple products in cart it is not working--------------------
Did you try following code:
{% assign productID = product.id %}
{% assign productInCart = 'false' %}
{% for item in cart.items %}
{% if item.product.id == productID %}
{% assign productInCart = 'true' %}
{% endif %}
{% endfor %}
Already, you assign "productInCart = false" by default
Now you only have to assign "productInCart = true" if product ID match with cart item so, just remore the else part from the loop.
You can use that variable anywhere in that file to check if the current product exists in cart or not.
Ex: If there are three products in cart where product IDs are 111, 222 and 333
Now, if you go to the product page where product ID is 222, then "productInCart" will be "true" because that product exists in cart.
In your code, "productInCart" will be "false" because, it becomes "false" again if current product ID not match with other cart items.
Thank you so much Tejas, have a great day!!!
Sorry to keep dragging this thread out, but I'm having some issues and I can't find a near-solution anywhere.
In my cart, I have a div for a Proof Request (custom items) with a dynamic Add To Cart button.
By clicking this button "ADD +" it will add the Proof Request product to the cart on the same page and change itself to a green non-clickable "ADDED ✓" button.
That all works great, BUT my issue is if there are multiple line items in the cart (without Proof Request added) then the "ADD +" button duplicates itself per line item. Of course once any of the duplicated "ADD +" buttons are clicked it runs fine and shows a single "ADDED ✓" button as it should.
Here's the snippet currently in my cart-main-footer.liquid: (removed styling)
{% for item in cart.items %}
{% if item.product.handle != 'proof-request' %}
<product-form class="atc-form1">
<form method="post" action="/cart/add"><input type="hidden" name="id" value="49144409260332">
<button type="submit" name="add">
ADD +
</button>
</form>
</product-form>
{% else %}
<style>
.atc-form1 {
display:none;
}
</style>
<div>
ADDED ✓
</div>
{% endif %}
{% endfor %}
my store URL for this is : https://americanbisonhats.com/cart
Thank you in advance!
@AmericanBison Try this code
{% assign prooBtnAdded = false %}
{% assign prooAdded = false %}
{% for item in cart.items %}
{% if item.product.handle != 'proof-request' and prooBtnAdded == false %}
{% assign prooBtnAdded = true %}
<product-form class="atc-form1">
<form method="post" action="/cart/add"><input type="hidden" name="id" value="49144409260332">
<button type="submit" name="add">
ADD +
</button>
</form>
</product-form>
{% else %}
{% if prooAdded == false %}
{% assign prooAdded = true %}
<style>
.atc-form1 {
display:none;
}
</style>
<div>
ADDED ✓
</div>
{% endif %}
{% endif %}
{% endfor %}
Let me know if it works for you.
Thank you for your time on this.
I had thoughts on using 'assign', but I've never used it before so wasn't sure how to implement it.
I tried the code and it seems to only be displaying the "ADDED ✓" no matter what, even without the product being in the cart.
However, I changed the != to == 'proof-request' and swapped the divs above and below, and it seems to be working.
Even with some minor tweaking, I definitely couldn't have achieved this without you so I greatly appreciate your help.
As 2024 wraps up, the dropshipping landscape is already shifting towards 2025's trends....
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024In today’s interview, we sat down with @BSS-Commerce to discuss practical strategies f...
By JasonH Nov 13, 2024