Show all products except items in the cart - Shopify - shopify

My aim is to show all products except the items in my cart essentially related items. I've tried several things but I just keep hitting a dead end.
So far:
This lists the product ids of each item in my cart:
{% assign all_product_ids = cart.items | map: 'product_id'| uniq %}
<pre>Product ids: {{ all_product_ids }}</pre>
This was my ureka moment however it's hardcoded, there must be a way to make it dynamic
{% for item in cart.items %}
{% if item.product.id == 1323872092227 %}
<div><h1>Nice sunglasses</h1></div><br>
{% endif %}
{% endfor %}
Still trying to get this to work
{% assign cart_item_ids = cart.items | map: 'product_id' %} // get array of cart item ids
{% for product in collections['all'].products%}
{% if cart_item_ids contains product.product_id %} // check if current product in loop's id is not in the array of cart_item_ids
<pre>Hi I'm in your cart</pre>
{% else %}
<pre>Hi I'm not in your cart</pre>
{% endif%}
{% endfor %}
Could someone point me in the right direction?
Thank you kindly

Related

Display product loop based on tag only in Shopify

I'm trying to display a product loop in a Collection template on Shopify. I want to pull all products with a specific tag 'Hydrate' to display within the Collection.
The template file is collection.custom.liquid and so far I have the below:
{% for product in collection.products %}
{% if product.tags contains "Hydrate" %}
Do Something
{% endif %}
{% endfor %}
But it doesn't display anything. I have confirmed I do have the tags setup correctly and I'm even using the exact same case sensitivity in case thats a thing.
If someone could point me in the right direction to display products from the tag 'Hydrate' in a custom collection template that would be great! Thanks so much
EDIT
I've now altered the code and moved it outside any pagination, and added for each product to the below. It now displays however it keeps displaying the same tags 5 times over on the page. Is there an issue with my syntax? Any ideas?
{% for product in collection.products %}
{% for product in products %}
{% if product.tags contains "Hydrate" %}
<p>{{ product.title }}</p>
{% endif %}
{% endfor %}
{% endfor %}
EDIT 2
In case anyone else has this same issue in future, the below code is what has ended up working and displays the correct tags, the correct amount of times:
{% assign products = collection.products %}
{% for product in products %}
{% if product.tags contains "Hydrate" %}
<p>{{ product.title }}</p>
{% endif %}
{% endfor %}
Your code is correct, so it might be a pagination issue. For example, there are no product with this tag in the first page but there are in the second one.

Shopify : Hide checkout button by product tag in cart

I want to hide the cart button when there are products with the Pre-order product tag and the Regular product tag in the cart.
This code, we were able to get the tags for all products in the cart.
{% for item in cart.items %}
{% assign carttag = item.product.tags %}
<p>{{ carttag }}</p>
{% endfor %}
However, the following code does not work.
{% if carttag contains 'Preorder' and carttag contains 'Regular' %}
<p>Stop!</p>
{% else %}
<input type="submit" name="checkout">
{% endif %}
How can I handle all looping values ​​as one?
You are on the right path, but your code has 2 problems.
You are overriding Cart Tags value for each product. So, you always have Product tags for last product in your cart.
product.tags return Array while you are treating it as string.
Fixing these issues your code will look like
{% assign cartTags = "" %}
{% for item in cart.items %}
{% assign joinedTags = item.product.tags | join: " " %}
{% assign cartTags = cartTags | append:" " | append:joinedTags %}
{% endfor %}
{% if cartTags contains 'string1' and cartTags contains 'string2' %}
{{cartTags}}
{% endif %}
What I have done here is, initialized an empty variable named cartTags. Then for each product in the cart, I convert the tags array to string using join filter and add them to all tags using append filter.
Product Tags
Join Filter
Append Filter

Shopify How to output a product that doesn't have a collection

I am trying to output all the products in a menu up to a certain number than output the collection types when that number is reached. So far that works fine, however I don't know how to output a product if the user does not put the product in a collection, I would like the products not entered into a collection to be in a template that I can link to in the menu that outputs the products not in a collection.
Here is my code so far.
{% for collection in collections %}
{% if shop.products_count <= 5 %}
{% for product in collections.[collection.title].products %}
{% capture productLink %}{{ product.url }}{% endcapture %}
{{product.title}}
{% endfor %}
{% elsif shop.products_count > 5 %}
{% capture collectionLink %}{{ collection.url }}{% endcapture %}
{{collection.title}}
{% else %}
You have no Products
{% endif %}
{% endfor %}
I would probably start by making a collection that contains all products in your shop (a smart collection where Product price > 0).
Then on the page where you want to display the products that are not in any collection (except the one we just created), try something like this:
{% for product in collections.all.products %}
{% if product.collections.size <= 1 %}
This product is not in any collections other than collections.all
{% endif %}
{% endfor %}

Shopify counts per tags

I am new to shopfiy.I have addded tags like Black, Blue, Green etc.
Now i want to show count like
Black(12)
Blue(1)
Green(4)
Does anybody help me out
Thanks
My answer to a similar question might help you with this: Shopify Tags total items
To get a count of all the products with a given tag you need to loop over the products and manually count them.
For example:
{% assign collection = collections.all %}
{% for tag in collection.all_tags %}
{% assign products_count = 0 %}
{% for product in collection.products %}
{% if product.tags contains tag %}
{% assign products_count = products_count | plus: 1 %}
{% endif %}
{% endfor %}
<p>{{ tag }} ({{ products_count }})</p>
{% endfor %}

Shopify If in collection then display this

I am trying to write a simple if statement, but always struggle with shopify's system.
Essentially I want it to do this:
{% if collection.product == 'discontinued' %}
This Product is Discontinued.
{% endif %}
If it's in this collection, then display this text/html. Otherwise it wouldn't display anything. This would be in the product.liquid template.
Any ideas?
This is what ended up working:
{% for c in product.collections %}
{% if c.handle == "discontinued" %}
This product is Discontinued
{% endif %}
{% endfor %}
You can create an array of the collections for a product using map on product.collections. This which will create a new array with your specified property, i.e. the handles of each collection.
You can then check if this new array contains the handle you want to work with.
{% assign productCollections = product.collections | map: "handle" %}
{% if productCollections contains 'your-collection-handle' %}
{% comment %} DoSomething {% endcomment %}
{% endif %}
So for your example:
{% assign productCollections = product.collections | map: "handle" %}
{% if productCollections contains 'discontinued' %}
This product is Discontinued
{% endif %}
You can map other fields if your case is different, such as the title.
I guess this will help any one, I have used in the sidebar of shopify website.
The current collection page will get checked by this below code.
<div class="row-fluid not-animated" data-animate="fadeInUp">
<div class="title">By Collections</div>
<form class="coll">
{% assign col_tags = collection.title %}
{% for collection in collections %}
<input type="radio" value="{{ collection.url }}" name="collections" {% if col_tags contains collection.title %} checked {% endif %} >{{ collection.title | escape }} <br/>
{% endfor %}
</form>
If I understand how liquid collections work in Shopify, you will need to iterate over all of your products.
You'd need to do something similar to this if you are working with collections directly:
{% for product in collection.product %}
{% if product.tags contains 'discontinued' %}
This product has been discontinued :(
{% endif %}
{% endfor %}
If you are just working with a single product you can probably just use the inner if liquid tag part.
References:
Collection.liquid
Product.liquid
You can indeed add discontinued products to a collection called discontinued.
When rendering a product, you could do as csaunders suggests, simply loop through all the products in the discontinued collection, and check if the id of the current product matches any of the products in that collection. If so, do what you must do. No need to use tags.