Skip to content

Instantly share code, notes, and snippets.

@schaeken
Last active November 18, 2021 13:40
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save schaeken/7c3f9e42e8c5f61a73f6 to your computer and use it in GitHub Desktop.
Save schaeken/7c3f9e42e8c5f61a73f6 to your computer and use it in GitHub Desktop.
Control the display order of tag groups lists in the collection sidebar using Supply's 'advanced tag filtering' - and control the sort order of the list items within that group using a link list.

Group Sorting:

  • There is a text field in the theme settings where you list the tag prefixes with no underscores. (ex: Size,Price,Color)
  • All groups must be mentioned in that input field in order to appear, if no groups are mentioned then it will go in alphabetical order.

Individual List Sorting:

  • This requires the use of a Link List in the Navigation area of the Admin
  • The list name is 'reorder_' with the tag prefix to represent the group of tags you want to manually sort. ie Reorder_Size
  • Each link inside this list is the full product tag with prefix and underscore directly the same as in the product page.
  • Only the lists that you want to sort manually need to be created or else it will just use the normal sorting of the list
  • See image below as an example:

http://snapify.shopify.com/28-30-zksi4-s8ox8.jpg

<div class="grid-uniform">
{% assign group_array = settings.group_array | split: ',' %}
{% for group in group_array %}
{% if cat_array contains group %}
{% capture array %}{% unless array == blank or array == "" %}{{ array }},{% endunless %}{{group | strip}}{% endcapture%}
{% endif %}
{% endfor%}
{% assign cat_array = array | split:',' %}
{% comment %}
If the theme setting for group array is empty then use the default cat_array order
{% endcomment %}
{% if group_array == blank %}
{% assign group_array = cat_array %}
{% endif %}
{% comment %}
Loop through tag categories
{% endcomment %}
{% for cat_item in group_array %}
{% assign tags_present = false %}
{% if cat_array contains cat_item %}
{% assign tags_present = true %}
{% endif %}
{% unless tags_present == false %}
{% assign linklist_title = cat_item | handleize | prepend: 'reorder_' %}
{% if linklists[linklist_title].links != '' %}
<div class="grid-item small--one-half medium--one-third">
<h3>{{ cat_item }}</h3>
<ul class="advanced-filters">
{% comment %}
Loop through collection tags
{% endcomment %}
{% for link in linklists[linklist_title].links %}
{% for tag in collection.all_tags %}
{% if tag contains link.title %}
{% assign cat = tag | split: '_' | first %}
{% if cat != tag and cat_item == cat %}
{% comment %}
Strip out tag category prefix and add/remove link for tag filtering
{% endcomment %}
{% if current_tags contains tag %}
<li class="advanced-filter active-filter" data-group="{{ cat_item }}" data-handle="{{ tag | handle }}">{{ tag | remove_first: cat_item | remove_first: '_' | link_to_remove_tag: tag }}</li>
{% else %}
<li class="advanced-filter" data-group="{{ cat_item }}" data-handle="{{ tag | handle }}">{{ tag | remove_first: cat_item | remove_first: '_' | link_to_add_tag: tag }}</li>
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
</ul>
</div>
{% endif %}
{% unless cat_item == '' or linklists[linklist_title].links.size > 0 %}
<div class="grid-item small--one-half medium--one-third">
<h3>{{ cat_item }}</h3>
<ul class="advanced-filters">
{% comment %}
Loop through collection tags
{% endcomment %}
{% for tag in collection.all_tags %}
{% assign cat = tag | split: '_' | first %}
{% if cat != tag and cat_item == cat %}
{% comment %}
Strip out tag category prefix and add/remove link for tag filtering
{% endcomment %}
{% if current_tags contains tag %}
<li class="advanced-filter active-filter" data-group="{{ cat_item }}" data-handle="{{ tag | handle }}">{{ tag | remove_first: cat_item | remove_first: '_' | link_to_remove_tag: tag }}</li>
{% else %}
<li class="advanced-filter" data-group="{{ cat_item }}" data-handle="{{ tag | handle }}">{{ tag | remove_first: cat_item | remove_first: '_' | link_to_add_tag: tag }}</li>
{% endif %}
{% endif %}
{% endfor %}
</ul>
</div>
{% endunless %}
{% endunless %}
{% endfor %}
</div>
{
"type": "text",
"label": "Type the sidebar's tag groups in a comma-separated list.",
"id": "group_array",
"info": "List items must be identical to the tag prefixes (no underscore), and have no spaces between commas.ie. Brand,Product Type,Burn Time (approx),Colour"
}
@Allan-Shand
Copy link

Where in settings_schema.json would you put the snippet

@aurearias
Copy link

I added mine here:

{
    "name": "Collection page",
    "settings": [

@peapolo
Copy link

peapolo commented Nov 11, 2015

Where exactly do you put it in the settings_schema.json as every time I try, it comes up with an error

@MacTolmie
Copy link

Peapolo, put a comma after the last curly bracket. Should fix you syntax error

@schaeken
Copy link
Author

Aurearias has it in the bag. These settings affect the collection page so it would make sense to place them in there. MacTolmie's correct, missing commas are the culprit generally.

@adrielw
Copy link

adrielw commented Jun 1, 2016

I did everything mentioned above, tested just the Size group.
But the website sidebar is blank. =O

@adrielw
Copy link

adrielw commented Jun 1, 2016

Oh, ignore me! I just got it to work. =)

Thanks schaeken!!

@PGaleMT
Copy link

PGaleMT commented Jun 29, 2016

I am hoping someone can help me. After making the code changes none of the filters are displaying on the page even after I created new navigation. Adrielw what was your fix or if anyone has any suggestion? Thanks in advance!

@ihasagithub
Copy link

My sidebar is also blank anyone have some tips on what I might have done wrong?

@staciecc
Copy link

staciecc commented Sep 7, 2016

Can someone please help me?
I am trying to re order my groups, but i keep getting the following message when i try to save:
This file contains the following errors:
Error: Invalid JSON: unexpected token

This is what i have added:
"type": "text",
"id": "group_array",
"label": "Gender,Product,Size,Color,Designer" done:
},

you can see where below:

{
"name": "Collection page",
"settings": [
{
"type": "text",
"id": "group_array",
"label": "Gender,Product,Size,Color,Designer",

},
"type": "select",
"id": "collection_prod_image_sizes",
"label": "Product image size",
"options": [
{

if anyone can please help i would appreciate it
Many thanks

@jeremystephencobb
Copy link

Looks like the issue is with the comma after "label": "Gender,Product,Size,Color,Designer",

The last item in a set of JSON curly braces does not get a comma.

@staciecc

@ErinRob
Copy link

ErinRob commented Oct 20, 2016

I used this code and my sidebar went blank too :/. Any suggestions?

@ymsstudios
Copy link

I added this to my client's website and everything seemed to look and work fine but for some reason, the collection page in the customizer stopped working after editing the sidebar to this code.

Any Help?!

@aptbs85
Copy link

aptbs85 commented Oct 23, 2017

Supply theme has changed since this fix. Any updates on how to reorder groups and tags?

@daleorrhelp
Copy link

Here is a workaround I created to allow you to reorder the sidebar categories. For some reason the array with group_array was not getting the appropriate category headings to compare to the category names. So, that's why this code above was no longer working.

My workaround only allows you to reorder the sidebar categories. It does not reorder the tag lists within the sidebar categories.

I am not a full time coder, just code here and there on the side with no formal training, so this may have some glitches. For instance, it seems to fail with tags having spaces and quotes in them, evidently (I had to change sizes to 1-1/2" from 1 1/2" for example). But, in general it works.

What I did was created a new linklist. Just go to navigation and create a new menu and put in your desired tag groups. (Brand, Size, etc.). Put the link for the new menut item (ie, "Brand") to homepage (it doesn't matter). You have to name these menu items the exact same as your tag categories or it won't work. You must name this new menu "SidebarNav". That is what the code will search for and parse through. The category you want first on the sidebar should be first and go on top when creating the new menu.

What happens is the code takes these menu items and creates a new loop. It says take menu item 1, and then when the old code category matches to that menu item, it executes the old code. It then goes on to menu item 2, etc. It only puts in the category when it matches to the new menu in the order of the new SidebarNav menu.

You do not need to modify your schema or settings file.

So, here is the entirety of the new collection-sidebar.liquid snippet



{% comment %}
  The code below relies on the advanced-tag-loop snippet.
  The snippet is already included in snippets/breadrumbs.liquid
  because it is needed there too, but if you remove
  breadcrumbs you need to include this:
{% endcomment %}


  {% include 'advanced-tag-loop' %}


{% if section.settings.collection_sidebar_filters == 'groups' %}
	{% if collection.all_tags.size > 0 %}

{% comment %}
Get the menu items from the SidebarNav link list to start a comparison to create new sidebar categories
{% endcomment %}

		{% for link in linklists.SidebarNav.links %}
     	

{% comment %}
Cycle through each of the tag headers just like before
{% endcomment %}


{% for cat_item in cat_array %}



{% comment %}
Compare the SidebarNav menu title to the cat item and only proceed if they match
{% endcomment %}

                  {% if cat_item == link.title %}


  					<div class="grid-uniform">
                            {% comment %}
                              Loop through tag categories
                            {% endcomment %}

                              <div class="grid-item small--one-half medium--one-third">
                                <h2 class="h3">{{ cat_item }}</h2>
                                <ul class="advanced-filters">
                                  {% comment %}
                                    Loop through collection tags
                                  {% endcomment %}
                                  {% for tag in collection.all_tags %}
                                    {% assign cat = tag | split: '_' | first %}
                                    {% if cat != tag and cat_item == cat %}
                                      {% comment %}
                                        Strip out tag category prefix and add/remove link for tag filtering
                                      {% endcomment %}
                                      {% if current_tags contains tag %}
                                        <li class="advanced-filter active-filter" data-group="{{ cat_item }}" data-handle="{{ tag | handle }}">{{ tag | remove_first: cat_item | remove_first: '_' | link_to_remove_tag: tag }}</li>
                                      {% else %}
                                        <li class="advanced-filter" data-group="{{ cat_item }}" data-handle="{{ tag | handle }}">{{ tag | remove_first: cat_item | remove_first: '_' | link_to_add_tag: tag }}</li>
                                      {% endif %}
                                    {% endif %}
                                  {% endfor %}
                                </ul>
                              </div>

          		</div>

                          <script>
                          $(function() {
                            var filters = $('.advanced-filter'),
                              el,
                              elGroup,
                              elHandle,
                              activeTagInGroup;

                            filters.on('click', function(e) {
                              el = $(this);
                              elGroup = el.data('group');
                              elHandle = el.data('handle');
                              activeTagInGroup = $('.active-filter[data-group="'+ elGroup +'"]');
                              // If the tag clicked is not already active and its group contains an active tag, we will swap tag within the group.
                              if ( !el.hasClass('active-filter')  && activeTagInGroup.size() ) {
                                e.preventDefault();
                                location.href = location.href
                                  // swap tag
                                  .replace(activeTagInGroup.data('handle'), elHandle)
                                  // go back to page 1
                                  .replace(/(&page=\d+)|(page=\d+&)|(\?page=\d+$)/, '');
                              }
                            });
                          });
                          </script>


                  {% endif %}   

                {% endfor %}
		{% endfor %}


{% endif %}

{% else %}
  <h2 class="h3">{{ 'collections.sidebar.shop_by' | t }}</h2>
  {% if template.name == 'collection' and collection.all_tags.size > 0 %}

    {% comment %}
      Provide a 'catch-all' link at the top of the list,
      we'd check against the collection.handle, product type, and vendor.
    {% endcomment %}
    <ul>
      {% if current_tags %}
        <li class="active-filter">
          {% comment %}
            Good for /collections/all collection and regular collections
          {% endcomment %}
          {% if collection.handle %}
            <a href="/collections/{{ collection.handle }}">
              {{ 'collections.sidebar.all' | t }}
              {% unless collection.title == 'Products' %}
              {{ collection.title }}
              {% endunless %}
            </a>

          {% comment %}
            Good for automatic type collections
          {% endcomment %}
          {% elsif collection.current_type %}
            <a href="{{ collection.current_type | url_for_type }}">{{ 'collections.sidebar.all_collection' | t: collection_title: collection.title }}</a>

          {% comment %}
            Good for automatic vendor collections
          {% endcomment %}
          {% elsif collection.current_vendor %}
            <a href="{{ collection.current_vendor | url_for_vendor }}">{{ 'collections.sidebar.all_collection' | t: collection_title: collection.title }}</a>
          {% endif %}
        </li>
      {% endif %}

      {% comment %}
        And for the good stuff, loop through the tags themselves.
        Strip the prepended categories if they happen to exist.
      {% endcomment %}
      {% for tag in collection.all_tags %}
        {% assign is_advanced_tag = false %}
        {% assign cat = tag | split: '_' | first %}
        {% unless cat == tag %}
          {% if cat_array contains cat %}
            {% assign is_advanced_tag = true %}
            {% if current_tags contains tag %}
              <li class="active-filter">{{ tag | remove_first: cat | remove_first: '_' }}</li>
            {% else %}
              <li>{{ tag | remove_first: cat | remove_first: '_' | link_to_tag: tag }}</li>
            {% endif %}
          {% endif %}
        {% endunless %}

        {% if is_advanced_tag == false %}
          {% if current_tags contains tag %}
            <li class="active-filter">{{ tag }}</li>
          {% else %}
            <li>{{ tag | link_to_tag: tag }}</li>
          {% endif %}
        {% endif %}
      {% endfor %}

    </ul>

  {% else %}
    <p>{{ 'collections.sidebar.no_tags' | t }}</p>
  {% endif %}


{% endif %}

@NS2212
Copy link

NS2212 commented Feb 8, 2018

I am not getting any error still filters are not showing on sidebar ???????

@adondai
Copy link

adondai commented Mar 29, 2018

The code given above is incomplete. Here is exactly how it should look in the settings_schema.json file:

{
    "name": "Filter Groups",
    "settings": [
  {
  "type": "text",
  "label": "Type the sidebar's tag groups in a comma-separated list.",
  "id": "group_array",
  "info": "List items must be identical to the tag prefixes (no underscore), and have no spaces between commas.ie. Brand,Product Type,Burn Time (approx),Colour"
  }
    ]
  },  

Here it is again with the surrounding code so you know exactly where to place it:

[
   {
    "name": "theme_info",
    "theme_name": "Supply",
    "theme_author": "Shopify",
    "theme_version": "2.4.3",
    "theme_documentation_url": "https:\/\/help.shopify.com\/manual\/using-themes\/themes-by-shopify\/supply",
    "theme_support_url": "https:\/\/support.shopify.com\/"
  },
  {
    "name": "Filter Groups",
    "settings": [
  {
  "type": "text",
  "label": "Type the sidebar's tag groups in a comma-separated list.",
  "id": "group_array",
  "info": "List items must be identical to the tag prefixes (no underscore), and have no spaces between commas.ie. Brand,Product Type,Burn Time (approx),Colour"
  }
    ]
  },  
  {
    "name": "Colors",
    "settings": [
      {
        "type": "header",
        "content": "General"
      },'

@cssimone17
Copy link

Does this work on other themes as well or will it only work on the supply theme?

@kagaim
Copy link

kagaim commented May 20, 2020

Thanks a million. I managed to implement it on a clients theme

@voyageisaverb
Copy link

Hello, I am trying to implement reordering sizes on the Prestige theme (i.e., XXS, XS, Small, Medium...). Has anyone had luck doing this? None of these suggestions have worked for me yet.

@raulros825
Copy link

@schaeken how we define cat_array? because it is blank value

@schaeken
Copy link
Author

It's defined in line 13 of advanced-tag-loop.liquid

{% assign cat_array = categories | split: '|' | uniq %}

@raulros825
Copy link

@schaeken can you send me new image for this? http://snapify.shopify.com/28-30-zksi4-s8ox8.jpg
It is blank... thanks!

@schaeken
Copy link
Author

Sorry, that was a long time ago. I have no idea what that image was about.

@JonaLusar
Copy link

Hello, I am also trying to implement reordering sizes on the Prestige theme (i.e., XXS, XS, Small, Medium...).Can anyone help me with this? All the suggestions I tried did not work in my case

Thanks!

@taylor-lindores-reeves
Copy link

Hello, I am also trying to implement reordering sizes on the Prestige theme (i.e., XXS, XS, Small, Medium...).Can anyone help me with this? All the suggestions I tried did not work in my case

Thanks!

Did you get it? I am also using this theme but need to reorder filters. Any help would be appreciated.

@taylor-lindores-reeves
Copy link

Hello, I am trying to implement reordering sizes on the Prestige theme (i.e., XXS, XS, Small, Medium...). Has anyone had luck doing this? None of these suggestions have worked for me yet.

Did you get it? I am also using this theme but need to reorder filters. Any help would be appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment