Display Items in Collection that Do Not Exist In Shopping Cart Shopify - shopify

I'm trying to iterate over items in the shopping cart and generate an upsell div with products NOT in the cart. Below is the code I have so far, but I run into issues when adding two items to cart running the loop twice generating the html twice. Any ideas on how to resolve it? I'm stumped.
{% for item in cart.items %} // iterates over items in cart
{% if item.product.id == 4456879040188 %} // checks if product id matches in item in cart
<div class="upsell-pop" style="text-align:center; width: 100%;">
<h4>Frequently bought together</h4>
{% for prod in collections.upsell.products %} // iterates products in collection upsell
{% unless prod.handle contains "product-name" %} // shows only prods that do not contain url handle
<div>
<span class="upsell-title">{{ prod.title }}</span>
<span class="upsell-price">{{ prod.metafields["meta"]["promo"] }} {{ prod.price | money }}</span>
<img src="{{ prod.featured_image | img_url: '200x' }}" />
<a class="btn-product" href="{{prod.url}}">View Product</a>
</div>
{% endunless %}
{% endfor %}
</div>
{% endif %}
{% endfor %}
Another thought is to somehow check to see if product is NOT in cart items inside to replace the existing "unless" statement, but not sure how to code it.
{% unless cart.items exist then %} // I know this is not correct syntax
<div>
<span class="upsell-title">{{ prod.title }}</span>
<span class="upsell-price">{{ prod.metafields["meta"]["promo"] }} {{ prod.price | money }}</span>
<img src="{{ prod.featured_image | img_url: '200x' }}" />
<a class="btn-product" href="{{prod.url}}">View Product</a>
</div>
{% endunless %}

On my mind, there are two steps here.
First, capture your cart content to get string to compare to when you loop through your upsell collection. This could be something like that:
{%- capture cart_items -%}
{% for item in cart.items %}
{{ item.product.handle }}{% unless forloop.last %} , {% endunless %}
{% endfor %}
{%- endcapture -%}
Then loop through your collection while checking your string does not contain the handle of the current product at each iteration:
{% for product in collections['upsell'].products %}
{% unless cart_items contains product.handle %}
{{ product.title }}
{% endunless %}
{% endfor %}
Remarks:
Snippet 1 => In line_item (here called item because it's shorter to write) object you may access product object attributes: item.product.product_attr_needed
Snippet 2 => To access directly a collection object using its handle you must use collections + square brackets containing collection handle + attribute. Here 'upsell' is the handle of your upsell collection.
Not tested but this should work.
HTH

Related

Concatenate string with html in liquid Shopify

{% assign price = product.price | money %}
{% capture dot_separator %} <span class='Button__SeparatorDot'></span> {% endcapture %}
{% capture addToCartText %} ADD TO CART {{ dot_separator }} {{ product_price_formatted }} {% endcapture %}
{{ addToCartText }}
My goal is to show the "addToCartText" text this way
Goal: ADD TO CART . 287$
Where the dot is created using css
Right now it's showing like this
Current output: ADD TO CART < span class='Button__SeparatorDot'></ span> 287$
How would I tell liquid to read the html as it is?
Try to use strip_html filter https://shopify.github.io/liquid/filters/strip_html/
like so:
{% capture dot_separator %} < span class='Button__SeparatorDot'></ span> {% endcapture %}
{% capture addToCartText %} ADD TO CART {{ dot_separator | strip_html }} {{ product_price_formatted }} {% endcapture %}
{{ addToCartText }}

Shopify: Product grid - how overwrite a Product STOCK-OUT msg, with "Coming Soon" if it's in a ComingSoon collection

Our existing theme displays a 'Stock Out' banner over the product image if there is no stock.
We want to change this banner to "Coming Soon" if the Product is in a ComingSoon Collection.
The theme's 'product-grid-item.liquid file contains:
<a href="{{ product.url | within: current_collection }}" class="product-grid-item">
<div class="product-grid-image">
<div class="product-grid-image--centered">
{% if sold_out %}
<div class="badge badge--sold-out"><span class="badge-label">{{ 'products.product.sold_out' | t }}</span></div>
{% endif %}
...
Which I changed to:
<!-- added ###For Test ### -->
{% assign found_collection = false %}
{% for collection in product.collections %}
{% if collection.handle contains 'Coming Soon' %}
{% assign found_collection = true %}
{% break %}
{% endif %}
{% endfor %}
{% if found_collection %}
<div class="badge badge--sold-out"><span class="badge-label">Coming Soon</span></div>
{% else %}
<div class="badge badge--sold-out"><span class="badge-label">{{ 'products.product.sold_out' | t }}</span></div> <!-- this is the original line of code -->
{% endif %}
<!-- end of added code -->
But is doesn't work, all 'no-stock' products display with the banner "Stock out"
Nb The actual Title of the ComingSoon collection is "Coming Soon ..." - so the contains statement should work?
We could change the code to look for a Tag, but admin maintaining a tag would be more work.
Most likely this is because 'Coming Soon' is not the correct handle. A handle is part of your URL that can be found under Collection SEO inside the admin panel
e.g. store.myshopify.com/collections/my-collection -> my-collection is the handle

Highlight the active collection button in shopify

I have a list of a collection I am calling in Shopify. I tried to use the link active tag but its not working. Here is my snippet
{% for collection in collections %}
{% unless collection.handle == 'frontpage' %}
<button type="button" class="btn btn-outline-info {% if link.active %}class='active'{% endif %}">{{ collection.title | escape | link_to: collection.url }}</button>
{% endunless %}
{% endfor %}
I am trying to add active class to the active collection, or the collection URL I am on presently.
I don't know what I am missing here.
Based on your comments, your code will function erratically. collection is a reserved variable in Shopify and by using the same variable in the loop, you might be changing the actual collection altogether. And secondly, link.active will only work inside a linklists loop.
Here's what you can do: change the assigning variable for the unit in your loop and validate if the handles are the same.
{% for thisCollection in collections %}
{% unless thisCollection.handle == 'frontpage' %}
<button type="button" class="btn btn-outline-info {% if thisCollection.handle == collection.handle %}class='active'{% endif %}">{{ thisCollection.title | escape | link_to: thisCollection.url }}</button>
{% endunless %}
{% endfor %}

Assigning a collection to a custom created collection page in Shopify?

I am using a free Venture theme on Shopify and i am trying to make a custom collection page.
I found a solution in stackoverflow but it was able to help someplace.
How to add collection.liquid to an existing page?
The summery of the solution is:
Copy everything that's in collection.liquid and paste it into a new snippet (let's say you call it collection-copy.liquid).
Then, in the page you want to add the collections page to, just add {% include 'collection-copy' %}
This solution worked well but there is one more issue for me. In the custom created page it says "Sorry, there are no products in this collection" In the customization of the same page there is a "collection" section. But in the "collection" section there is no option to choose a collection. There is only "Enable tag filtering" and "Enable sorting" check boxes.
Webpage: https://mottomfreedom.com/pages/less-is-more
Do you have any idea of assigning a collection with this custom created snippet?
{% paginate collections[settings.frontpage_collection].products by 20 %}
<div class="page-width">
<header class="grid medium-up--grid--table section-header small--text-center">
<div class="grid__item medium-up--one-half section-header__item">
<h1 class="section-header__title">
{{ collection.title }}
{% if current_tags %}
– {% assign title_tags = current_tags | join: ', ' %}
{{ title_tags }}
{% endif %}
</h1>
{% if collection.description != blank %}
<div class="section-header__subtext rte">
{{ collection.description }}
</div>
{% endif %}
</div>
<div class="grid__item medium-up--one-half medium-up--text-right section-header__item">
{% section 'collection-filters' %}
</div>
</header>
<div class="grid grid--no-gutters grid--uniform">
{% for product in collection.products %}
<div class="grid__item small--one- medium-up--one-third">
{% include 'product-card', product: product %}
</div>
{% else %}
{% comment %}
Add default products to help with onboarding for collections/all only.
The onboarding styles and products are only loaded if the
store has no products.
{% endcomment %}
{% if shop.products_count == 0 %}
<div class="grid__item">
<div class="grid grid--no-gutters grid--uniform">
{% assign collection_index = 1 %}
{% for i in (1..10) %}
{% case i %}
{% when 7 %}
{% assign collection_index = 1 %}
{% when 8 %}
{% assign collection_index = 2 %}
{% when 9 %}
{% assign collection_index = 3 %}
{% when 10 %}
{% assign collection_index = 4 %}
{% endcase %}
<div class="grid__item small--one-half medium-up--one-fifth">
<a href="/admin/products" class="product-card">
<div class="product-card__image-container">
<div class="product-card__image-wrapper">
<div class="product-card__image">
{% capture current %}{% cycle 1, 2, 3, 4, 5, 6 %}{% endcapture %}
{{ 'product-' | append: current | placeholder_svg_tag: 'placeholder-svg' }}
</div>
</div>
</div>
<div class="product-card__info">
<div class="product-card__name">{{ 'homepage.onboarding.product_title' | t }}</div>
<div class="product-card__price">
$19.99
</div>
</div>
<div class="product-card__overlay">
{% assign view_string_length = 'products.product.view' | t | size %}
<span class="btn product-card__overlay-btn {% if view_string_length > 8 %} btn--narrow{% endif %}">{{ 'products.product.view' | t }}</span>
</div>
</a>
</div>
{% assign collection_index = collection_index | plus: 1 %}
{% endfor %}
</div>
</div>
{% else %}
{% comment %}
If collection exists but is empty, display message
{% endcomment %}
<div class="grid__item small--text-center">
<p>{{ 'collections.general.no_matches' | t }}</p>
</div>
{% endif %}
{% endfor %}
</div>
{% if paginate.pages > 1 %}
<div class="pagination">
{{ paginate | default_pagination | replace: '« Previous', '←' | replace: 'Next »', '→' }}
</div>
{% endif %}
</div>
{% endpaginate %}
You are right about giving some time before accepting an answer :)) The solution worked but forced me to create 1 page and 4 liquid files per collection. And at the end, i figured out that some sections like "collection.list" doesn't directs to the page which i have created. I think you were talking about this at the beginning of the answer :)
After that, i found a much better solution. Just creating a new section.liquid file and placing it in "collection.liquid" with an "if" statement solved my problem.
{% if collection.handle == 'less-is-more' %}
{% section 'custom-featured-products-LESSisMORE' %}
{% endif %}
But in any way, i'm grateful for your interest. Thank you very much Dave.
It looks like there's nothing defining the collection variable anywhere.
I would suggest changing the beginning of your code snippet from:
{% paginate collections[settings.frontpage_collection].products by 20 %}
To:
{% assign collection = collections[settings.frontpage_collection] %}
{% paginate collection.products by 20 %}
There is an implicit collections variable whenever you're on a page that includes /collections/[something] in the URL, but when you're on a URL that's /page/[something], you have an implicit page variable in Liquid instead.
Note: if the collection set in your theme's value for settings.frontpage_collection isn't the one you want, you can possibly:
a. Change the value using the 'Customize' link beside your theme (most easily found on the /admin/themes page), useful if you're not going to use that setting for anything else;
b. Hard-code a collection handle, eg: collections['i-am-sure-this-will-never-change'], but hard-coded strings are ugly and should generally be avoided;
c. Create your own theme setting by adding an entry to config/settings_schema.json - see https://help.shopify.com/en/themes/development/theme-editor/settings-schema if you're still getting up to speed with custom theme settings; or
d. If all your content is in a section, you can use section settings (similar to theme settings) to make a variable that's tied specifically to just that block of code.
If you need to make these special pages for multiple collections, and each of these pages is largely reusing the same code, you can make your life easier by moving the common code to a snippet and passing variables to it from your page template. To do so:
Create a file in the 'snippets' folder of your theme. (For this example, let's say the file is called collection-in-page.liquid. We will be passing a collection into this snippet, so you can remove the assign statement.
In your page-specific template, figure out what the collection handle is going to be
a. This might be hard-coded, or it might be something you could look up by using metafields or tags on the page. Examples:
{% assign collection_handle = 'hardcoded-handle' %}, {% assign collection_handle = page.metafields.related_items.collection %}
In your page template, include the snippet you created. I find it helps to explicitly pass any variables I want to use, like so:
{% include 'collection-in-page', collection: collections[collection_handle] %}
Hope this helps!

Display links to other colors on Shopify product page

I have an online store on Shopify with one product in 10 different colours. So for SEO purposes, I've created 10 products like T-Shirt Black, T-Shirt Blue, T-Shirt Red, etc. To each product, I assigned its own tag and unique description.
What I am trying to do is to link all products between each other from each product page, for instance, the current viewing product is T-Shirt Black, on that page I'd like to show 10 clickable colours/swatch with images of each colour and to the current viewing one add the active class, when clicked on another colour it takes user to another product page. How do I make it happen?
Any help would be much appreciated!
I'd like to share with other people who might need to solve the same task.
Here's what I've done to get this work;
First, I've added a tag to each product based on its collection, in an example for all products in collection SMALL I've added tag small, then I had to write this code to get a list of all the products with the same tag.
{% if product.tags contains "small" %}
{% assign current_product_tag = "small" %}
{% elsif product.tags contains "medium" %}
{% assign current_product_tag = "medium" %}
{% elsif product.tags contains "large" %}
{% assign current_product_tag = "large" %}
{% endif %}
{% assign current_product = product %}
<div id="sw_container">
<p class="sw_title">Select Colour</p>
<ul class="sw_list">
{% for product in collections.all.products %}
{% if product.tags contains current_product_tag %}
<li class="sw_item{% if product.handle == current_product.handle %} active{% endif %}">
<a title="{{ product.title | escape }}" href="{% if product.handle == current_product.handle %}#{% else %}{{ product.url }}{% endif %}">
<img src="{{ product.images.last | product_img_url: 'small' }}" alt="{{ product.title | escape }}">
</a>
</li>
{% endif %}
{% endfor %}
</ul>
</div>