Shopify: Filter by Multiple Tags at Same time - shopify

Ok so I am creating menu for filtering products in order to keep my tags and submenu from being extremely long.
I have created a dropdown filter for my main categories (I have added an unless to eliminate sub-level category tags)
I then have a sub-category menu that only shows when "All Categories" isn't selected.
This then generates a sub-category filter for all tags in that category. See below for code:
<div class="text-center">
<div class="browseby" style="display:inline;padding:20px;">
<div class="clearfix filter" style="float:left;">
Browse By Category <select class="coll-filter">
<option value="">All</option>
{% for tag in collection.all_tags %}
{% unless tag == 'HP' or tag == 'Latex' or tag == 'Latex 570' or tag == 'Parts' or tag == 'Aqueous Media' or tag == 'Latex Media' or tag == 'Solvent Media' %}
{% if current_tags contains tag %}
<option value="{{ tag | handle }}" selected>{{ tag }} ({{ collection.products_count }})</option>
{% else %}
<option value="{{ tag | handle }}">{{ tag }}</option>
{% endif %}
{% endunless %}
{% endfor %}
</select>
</div>
{% if current_tags %}
<div class="clearfix filter" style="float:left; padding-left:20px">
Browse By Sub-Category <select class="coll-filter">
<option value="">All</option>
{% for tag in collection.tags %}
{% unless current_tags contains tag %}
{% if current_tags contains tag %}
<option value="{{ tag | handle }}" selected>{{ tag }} ({{ collection.products_count }})</option>
{% else %}
<option value="{{ tag | handle }}">{{ tag }}</option>
{% endif %}
{% endunless %}
{% endfor %}
</select>
</div>
{% else %}
{% endif %}
</div>
Now the problem is when I select the sub-category filter the tag isn't shown as selected.
Browse By Category shows All and then inside the dropdown I have (All, Category1, Category2, Category3..)
If i selected Category3 the page reloads and shows Browse By Category: Category3
Browse By Sub-Category shows All and then inside the dropdown I have (All, SubMenu1, SubMenu2, SubMenu3)
If I select SubMenu2 the products all filter.
Browse By Category still shows: Category3
But Browse By Sub-Category shows: All
Inside the dropdown menu I have (All, SubMenu1, SubMenu3) <-- The category that is being filtered disappears from the list, but does filter products.
If this is confusing let me know.

RESOLVED!!
I found the problem in the above code. The {% unless %} tag in the second filter was causing the problem. The unless was to filter out the main category filter but ended up making the selected option not available. I made my Main Categories start with cat- and the sub-level categories start with sub-
Now the code works mostly... Still having issues when using the sub-category and then filtering back to a main category. The URL keeps the sub-cat and just adds a new main category...
Anyway for those that are interested here is my new code:
<div class="text-center">
<div class="browseby" style="display:inline;padding:20px;">
<div class="clearfix filter" style="float:left;">
Browse By Category <select class="coll-filter">
<option value="">All</option>
{% for tag in collection.all_tags %}
{% if tag contains 'cat-' %}
{% assign tagName = tag | remove: 'cat-' %}
{% if current_tags contains tag %}
<option value="{{ tag | handle }}" selected>{{ tagName }}</option>
{% else %}
<option value="{{ tag | handle }}">{{ tagName }}</option>
{% endif %}
{% endif %}
{% endfor %}
</select>
</div>
<div class="clearfix filter" style="float:left; padding-left:20px">
Browse By Type: <select class="coll-filter">
<option value="">All</option>
{% for tag in collection.tags %}
{% if tag contains 'sub-' %}
{% assign tagName = tag | remove: 'sub-' %}
{% if current_tags contains tag %}
<option value="{{ tag | handle }}" selected>{{ tagName }}</option>
{% else %}
<option value="{{ tag | handle }}">{{ tagName }}</option>
{% endif %}
{% endif %}
{% endfor %}
</select>
</div>
</div>

Related

Capitalizing items in a dropdown box using Liquid

First time using Liquid.. I am an old-time Vbasic programmer.
I had added some customization to our Shopify Store. I have worked out the basic details for the mechanics of the code and have it working, but I would like the items to be properly capitalized when it creates the dropbox.
{% if section.settings.show_collection_tags and section.settings.collection_tags == 'dropdown' %}
{% if collection.url.size == 0 %}
{% assign collection_url = routes.all_products_collection_url %}
{% else %}
{% assign collection_url = collection.url %}
{% endif %}
{% assign has_tags = false %}
{% capture tags_html %}
<span class="tags filter">
<label for="filter-by">{{ 'collections.general.filter_by' | t }}:</label>
<select id="filter-by" class="redirect">
<option value="{{ collection_url }}">{{ 'collections.general.all_items' | t }}</option>
{% for tag in collection.all_tags %}
{% unless BadTags contains tag %}
<option value="{{ collection_url }}/{{ tag | handle }}" {% if current_tags contains tag %}selected="selected"{% endif %}>{{ tag }}</option>
{% assign has_tags = true %}
{% endunless %}
{% endfor %}
</select>
</span>
{% endcapture %}{% if has_tags %}{{ tags_html }}{% endif %}
{% endif %}
I tried using | Capitalize following {{{ tag | handle but that didn't work.
Where am I going wrong?

Can we use multiple rendered queryset in template with multiple for loops in scriptlet in Django framework?

I am trying to fetch 2 tables data in a template using the querysets I rendered from my views.py, now I want to fetch both queryset in a template using scriptlet for loop in select field but it is not working. One select field in the template is showing the data but second select field is not showing the data. I tried and find that only one at a time is showing the data while 2nd is not working.
This is my views.py :
def assignWork(request):
man=Coordinator.objects.filter(designation="Manager")
t=Task.objects.all()
return render(request,"MyAdmin/assignWork.html",{"key2":t},{"key1":man})
This is my template assignWork.html in which I am trying to use both.
<form action="assignTask" method="POST">{% csrf_token %}
<label> Tasks</label>
<br>
<select required="">
<option value="">Select</option>
{% if key2 %}
{% for var1 in key2 %}
<option value="{{ var.id}}">
{{ var1.company.cname }}</option>
{% endfor %}
{% endif %}
</select>
<br>
<label> Manager</label>
<br>
<select name=txtname required="">
<option value="">Select Manager</option>
{% if key1 %}
{% for var in key1 %}
<option>
{{ var.name }}</option>
{% endfor %}
{% endif %}
</select>
<b> <- Back</b>
<input type="submit" name="submit" value="Edit Details">
</form>

Shopify - Is it possible to filter by 'Vendor'

it seems Shopify only allows for filtering by tags. If you want to filter by Vendor (which is a standard Shopify Field) you first need to create tags with the same name and manually use those tags in the filter sidebar.
Is this correct? Seems very unnecessary and more importantly makes dynamic updates difficult.
After almost 2 days of trial and error, plus lots of digging into forums and the documentation, I came up with this bit of code to create a working Vendor filter.
Please note the following:
The classes and IDs were created by the Modular Shopify theme. I just reused them, please use the appropriate classes/IDs for your theme.
The Vendor filter does not work with the tag filter or with the sort by option. Those were hidden as explained below.
<div class="grid-filter block">
{% comment %} New Vendor Filter {% endcomment %}
<div class="tag-filter block">
<label for="#vendorFilter">Browse by Vendor</label>
<span class="selectArrow"></span>
<select class="vendorFilter filter" id="vendorFilter" onChange="window.location.href=this.value">
<option value="/collections/all">All</option>
{% for product_vendor in shop.vendors %}
{% if collection.current_vendor contains product_vendor %}
<option value="{{ product_vendor | url_for_vendor }}" selected>{{ product_vendor }}</option>
{% else %}
<option value="{{ product_vendor | url_for_vendor }}">{{ product_vendor }}</option>
{% endif %}
{% endfor %}
</select>
</div>
{% comment %} END New Vendor Filter {% endcomment %}
{% if show_filter == true and collection.current_vendor == blank %}
<div class="tag-filter block">
<label for="#tagFilter">{{ 'collections.tag_filtering.filter_label' | t }}</label>
<span class="selectArrow"></span>
<select class="tagFilter filter" id="tagFilter">
<option value="/collections/{{ collection.handle }}">{{ 'collections.tag_filtering.default_filter' | t }}</option>
{% for tag in collection.all_tags %}
{% if current_tags contains tag %}
<option value="/collections/{{ collection.handle }}/{{ tag | handle }}" selected>{{ tag }}</option>
{% else %}
<option value="/collections/{{ collection.handle }}/{{ tag | handle }}">{{ tag }}</option>
{% endif %}
{% endfor %}
</select>
</div>
{% endif %}
{% if show_sort_filter == true and collection.current_vendor == blank %}
<div class="collectionGrid-filter block">
<label for="#collectionFilter">{{ 'collections.sorting_dropdown.label' | t }}</label>
<span class="selectArrow"></span>
{% assign sort_by = collection.sort_by %}
<select class="filter" id="collectionFilter">
<option value="">{{ 'collections.sorting_dropdown.all' | t }}</option>
<option value="best-selling" {% if sort_by == "best-selling" %}selected{% endif %}>{{ 'collections.sorting_dropdown.best_selling' | t }}</option>
<option value="price-ascending" {% if sort_by == "price-ascending" %}selected{% endif %}>{{ 'collections.sorting_dropdown.price_ascending' | t }}</option>
<option value="price-descending" {% if sort_by == "price-descending" %}selected{% endif %}>{{ 'collections.sorting_dropdown.price_descending' | t }}</option>
<option value="title-ascending" {% if sort_by == "title-ascending" %}selected{% endif %}>{{ 'collections.sorting_dropdown.title_ascending' | t }}</option>
<option value="title-descending" {% if sort_by == "title-descending" %}selected{% endif %}>{{ 'collections.sorting_dropdown.title_descending' | t }}</option>
<option value="created-ascending" {% if sort_by == "created-ascending" %}selected{% endif %}>{{ 'collections.sorting_dropdown.created_ascending' | t }}</option>
<option value="created-descending" {% if sort_by == "created-descending" %}selected{% endif %}>{{ 'collections.sorting_dropdown.created_descending' | t }}</option>
</select>
</div>
{% endif %}
</div>
Here are the important things worth mentioning:
The onChange="window.location.href=this.value" is a bit of JavaScript necessary to grab the value from the vendor dropdown and to update the web address (URL).
The for product_vendor in shop.vendors is a bit of Liquid necessary to grab all vendors and provide them one at a time to the dropdown.
The if collection.current_vendor contains product_vendor is also a bit of Liquid necessary when the vendor filter is in use. It checks for an active vendor filter and selects it in the dropdown after the page has reloaded.
The {{ product_vendor | url_for_vendor }} provides the URL used by the JavaScript in #1 above.
The {{ product_vendor }} provides the actual vendor name to the list.
The and collection.current_vendor == blank tells the page to hide the Tag and Sort dropdowns if a vendor filter was selected.
Here are the Shopify Docs which really helped me. This assumes you already understand the for loop and if statements which are a basic component of programming.
Understanding shop.vendors
Understanding collection.current_vendor
Understanding url_for_vendor
Understanding product_vendor
Understanding blank
Shopify doesn't allow for filtration by vendor at the moment (while keeping the collection relation to the vendor).
This was mentioned in a recent live Q&A with Shopify and it was confirmed that they are testing this. More info can be seen here: https://youtu.be/MDqDIIyxIcU?t=2078
You have to stick with the tags for now.

Remove specific product option inside a dropdown

I wonder if is it possible to remove specific product option dropdown and display it inside a <p> tag or just a normal string? Imagine I have 3 product options:
Color
Size
Type
We all know that all those options will be displayed inside a dropdown menu. What about like I wanted to display the Size option as a normal string or text? How can we do that?
Here's an image to make it clearer.
product.liquid
<select name="id" id="ProductSelect" class="product-single__variants">
{% for variant in product.variants %}
{% if variant.available %}
<option {% if variant == product.selected_or_first_available_variant %} selected="selected" {% endif %} data-sku="{{ variant.sku }}" value="{{ variant.id }}">{{ variant.title }} - {{ variant.price | money_with_currency }} </option>
{% else %}
<option disabled="disabled">
{{ variant.title }} - {{ 'products.product.sold_out' | t }}
</option>
{% endif %}
{% endfor %}
</select>
I just found the answer for this. I'll post it in here to help others with the same problem as I have.
In product.liquid:
<!-- product options -->
{% capture option_titles %}Size,Flavor{% endcapture %}
{% assign option_titles = option_titles | split:',' %}
{% for option in product.options %}
{% if option_titles contains option %}
{% capture option_index %}option{{ forloop.index }}{% endcapture %}
{% assign option_values = product.variants | map: option_index | uniq %}
{% if option == 'Flavor' and option_values.size > 1 %}
<label for="option">{{ option }}</label>
{{ option_values | join:', ' }}
{% elsif option_values.size == 1 %}
<label for="option">{{ option }}</label>
{{ option_values }}
{% endif %}
{% endif %}
{% endfor %}
<!-- end product options --->
You have to modify product.liquid template and instead of that drop down you have to create that as a LI or Text the you are set for that . 😃

Shopify - Hiding sold out variants on dropdown (regular solution not working)

On my Shopify store I need to be able to hide sizes in the dropdown that are no longer available. I have tried multiple times adding the code that Shopify suggests here but I am using the Retina Out of the Sandbox theme and add that code to the product-form.liquid file and what happens is only 1 size becomes available no matter what. My store is in dire need of this feature because we sell tons of closeout shoes no longer available so when a customer searches for a size products that have a sold out size 9 still show because it is not hidden on the dropdown, it just says Sold Out, here is my code. Apologies if the formatting is not so nice looking, this is what came with my theme.
product-form.liquid
{% if product.available %}
<form action="/cart/add" method="post" class="clearfix product_form" data-money-format="{{ shop.money_format }}" data-shop-currency="{{ shop.currency }}" id="product-form-{{ product.id }}">
{% if settings.display_inventory_left %}
<div class="items_left">
{% if product.variants.first.inventory_management == "shopify" and product.variants.first.inventory_quantity > 0 %}
<p><em>{{ product.variants.first.inventory_quantity }} {{ settings.inventory_left_text | escape }}</em></p>
{% endif %}
</div>
{% endif %}
{% if product.options.size > 1 %}
<div class="select">
<select id="product-select-{{ product.id }}" name='id'>
{% for variant in product.variants %}
<option {% if variant == product.selected_or_first_available_variant %}selected="selected"{% endif %} value="{{ variant.id }}">{{ variant.title }}</option>
{% endfor %}
</select>
</div>
{% elsif product.options.size == 1 and (product.variants.size > 1 or product.options[0] != "Title") %}
<div class="select">
<label>{{ product.options[0] }}:</label>
<select id="product-select-{{ product.id }}" name='id'>
{% for variant in product.variants %}
<option {% if variant == product.selected_or_first_available_variant %}selected="selected"{% endif %} value="{{ variant.id }}">{{ variant.title }}</option>
{% endfor %}
</select>
</div>
{% else %}
<input type="hidden" name="id" value="{{ product.variants.first.id }}" />
{% endif %}
{% if settings.display_product_quantity %}
<div class="left">
<label for="quantity">Quantity:</label>
<input type="number" min="1" size="2" class="quantity" name="quantity" id="quantity" value="1" />
</div>
{% endif %}
<div class="purchase clearfix {% if settings.display_product_quantity %}inline_purchase{% endif %}">
{% if settings.cart_return == 'back' %}
<input type="hidden" name="return_to" value="back" />
{% endif %}
<input type="submit" name="add" value="{{ settings.add_to_cart_text | escape }}" class="action_button add_to_cart" />
</div>
</form>
{% if product.variants.size > 1 or product.options.size > 1 %}
<script type="text/javascript">
// <![CDATA[
$(function() {
$product = $('#product-' + {{ product.id }});
new Shopify.OptionSelectors
("product-select-{{ product.id }}",{
product: {{ product | json }},
onVariantSelected: selectCallback{% if product-form == 'product' %},
enableHistoryState: true{% endif %}
});
{% if product.options.size == 0 %}
{% for variant in product.variants %}
{% unless variant.available %}
jQuery('.single-option-selector option').filter(function() { return jQuery(this).html() === {{ variant.title | json }}; }).remove();
{% endunless %}
{% endfor %}
jQuery('.single-option-selector').trigger('change');
{% endif %}
// ]]>
</script>
{% endif %}
{% endif %}
A couple of small things I noticed:
{% if product.options.size == 0 %} should be {% if product.options.size == 1 %} (see here).
You're missing the closing brackets for $(function() {.... You need }); before the closing </script> tag.
This seems to work for me now:
{% if product.variants.size > 1 or product.options.size > 1 %}
<script type="text/javascript">
// <![CDATA[
$(function() {
$product = $('#product-' + {{ product.id }});
new Shopify.OptionSelectors
("product-select-{{ product.id }}",{
product: {{ product | json }},
onVariantSelected: selectCallback{% if product-form == 'product' %},
enableHistoryState: true{% endif %}
});
{% if product.options.size == 1 %} // *** should be 1, not 0 ***
{% for variant in product.variants %}
{% unless variant.available %}
jQuery('.single-option-selector option').filter(function() { return jQuery(this).html() === {{ variant.title | json }}; }).remove();
{% endunless %}
{% endfor %}
jQuery('.single-option-selector').trigger('change');
{% endif %}
}); // *** missing closing brackets here ***
// ]]>
</script>
{% endif %}