Git Product home page Git Product logo

shopify-product-filter's Introduction

Shopify Product Filter

A simple, jQuery based product variant filter for Shopify stores ๐Ÿ‘• ๐Ÿ”

๐Ÿš€ DEMO: See it in action at devtees.com. (Note: I'm no longer using this on devtees.com because I switched to a print on demand service that doesn't limit the sizes in stock; but if you have implemented the filter somewhere, let me know and I can add it to the Readme under an examples section!)

img

What is it for?

Ever wish you could show your Shopify customers a filtering option on your home or collection page so they can see available sizes without having to click through to the individual product page? Well, I did.

This adds very basic functionality to your Shopify store to filter products based on size, or whatever product variant you wish to filter by.

The filtering menu hides items that are unavailable in the selected variant, and displays the items that are availble in that variant.

Usage

By default the filter will work with a size variant if that's the only variant your products have available. Otherwise you can pass an option number to pick which variant to filter by.

Where to put the code

  1. Put snippets/size-filter.liquid and snippets/size-filter-dynamic.liquid into your Shopify snippets directory.

  2. Put assets/product-filter.js into your assets directory, and be sure to include it in your theme's layout/theme.liquid file in the document <head> in script tags. Alternatively, you could instead copy the contents inside the $(document).ready(function(){...}); and paste it into your existing scripts.js file. Also, be sure you are including jQuery in your shop's theme as well.

  3. If you are going use the provided default styles, then include assets/product-filter.css.liquid in your assets directory, and be sure to link to it from your layout/theme.liquid file. Alternatively you could simply include the styles in your existing .css.liquid file.

  4. Next you need to output the variant options as classnames in your product-loop.liquid file so the filter buttons will have something to trigger. Add the following values to the class attribute on your div.product or corresponding markup: js-size-filter {% for variant in product.variants %}{% if variant.available %}size-{{ variant.title }} {% endif %}{% endfor %}

  5. Finally, you need to include the size-filter.liquid (or size-filter-dynamic.liquid if you want a dynamically generated menu; more on that below). Just use {% include 'size-filter' %} to include the filter menu wherever you'd like it to appear. In devtees.com example I've put it in templates/index.liquid, but you could instead put it in templates/collection.liquid if you wanted it to appear on every collection page. I just wanted it on the frontpage collection. E.g.:

<!-- start featured product -->

  <div class="clearfix helper-section">

    {% include 'size-filter' %}

    {% if collections[settings.frontpage_collection].products.size == 0 %}

    {% else %}

Static or dynamic menu?

As mentioned above, there are two options for generating the menu itself. There is a dynamic option that uses Liquid tags and outputs the available variants in an array, and there is a static option that is hard coded, and you can edit accordingly.

Why in BFE would I want to use a static, hard coded version? Great question. The answer is because you might not like the order that the dynamic version outputs your variants in, and apart from using the sort filter to output them alphabetically, there's not much in the way of options there. Personally, for my use on devtees.com as a size filter, I wanted them to appear in logical order, which was impossible dynamically. Hence the hard coded option.

Styling

I've included a set of styles based on my use at devtees.com. However, if you want to roll your own styles, just ignore the assets/product-filter.css.liquid file, and style in your own stylesheet however you like.

Note: the default styles I've provided use Sass, as it's now supported by Shopify css files with the .css.liquid extension.

Limitations

One of the biggest limitations of this filter is that - because of how Shopify product variants work - in order for the filter to work your product variants must be formatted just right, or else the filter will fail. The filter relies on outputing your variant titles to a classname on the product listing, which it looks for to either hide or show when a filter nav button is clicked.

Consequently you must:

  1. Not include spaces in your variant names
  2. Write your variant names as you want them to appear in the filter menu

PROTIP: If you need to edit your product variants en masse, you can export your products from the Shopify Admin "Product" dashboard as a CSV file, do a find & replace to mass edit, them import it back into your shop. More on that in the Shopify docs here.

Resources

  • shopify_theme - Shopify's CLI tool for directly uploading your locally edited theme files to your shop.

Contributing

Found a bug? Want to improve the jQuery or Liquid logic? Pull requests welcome!

See the contributing docs for contribution guidelines and more ways to contribute.

License

This project is available freely under the terms of The MIT License.

shopify-product-filter's People

Contributors

jglovier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

shopify-product-filter's Issues

Using the size-filter-dynamic on collection pages

I noticed that out of the box, the liquid snippets sets the collection default to front page. I only want sizes being returned on the collection I'm currently viewing. So, in size-filter-dynamic.liquid, I changed this:

{% for product in collections.frontpage.products limit: limit %}

to this:

{% for product in collection.products limit: limit %}

Which seems to work. Is this the right way of going about returning size variants for each collection?

Function doesnt work

Hello!
I implemented all things but i can't see your design and the js doesn't work i select my size and doesnt make anything, i don't know what happened, i followed all steps but i dont found product-loop

Please help me! Thank you

Variant options aren't showing up at all

I followed all the instructions and the only variant filter option that I see it "All" . My products are not on the home page but on an inner page, could it be that this line "{% for product in collections.frontpage.products limit: limit %}" in size-filter-dynamic.liquid is preventing the variants from showing up?

Unable to get step 4 working.

Next you need to output the variant options as classnames in your product-loop.liquid file so the filter buttons will have something to trigger. Add the following values to the class attribute on your div.product or corresponding markup: js-size-filter {% for variant in product.variants %}{% if variant.available %}size-{{ variant.title }} {% endif %}{% endfor %}

Hi I'm unable to get this step working. Can you help here please? Initially there was no product-loop.liquid and I added it from the shopify templates and then tried to customize it but in vain. Can you help here?

Problem getting it to work

I really love this code and would love to implement it into my site, but I'm have a problem figuring out exactly what I'm not doing right. It seems like I'm almost there... but there might be something that I'm missing which is preventing it from working 100%.

The steps that I did are as follows:

I added size-filter.liquid & size-filter-dynamic.liquid into my Shopify snippets directory.

I added product-filter.js into my assets directory and put {{ 'product-filter.js' | asset_url | script_tag }} into my theme.liquid in my layout directory directly under

I added product-filter.css.liquid in my assets directory, and put {{ 'product-filter.css' | asset_url | stylesheet_tag }} into my theme.liquid in my layout directory directly under the {{ 'product-filter.js' | asset_url | script_tag }}

I didn't have a product-loop.liquid in my Shopify theme (minimal), so I found where my products are and added js-size-filter {% for variant in product.variants %}{% if variant.available %}size-{{ variant.title }} {% endif %}{% endfor %} in the class part. It looks as follows: <div class="grid__item {{grid_item_width}}{% if sold_out %} sold-out{% endif %}{% if on_sale %} on-sale{% endif %} js-size-filter {% for variant in product.variants %}{% if variant.available %}size-{{ variant.title }} {% endif %}{% endfor %}"> This is in my product-grid-item.liquid under my snippets directory. When I look at my page source code, I see that it is working (or so I think)... Every item has the following (or something similar depending on the sizes/colors offered) <div class="grid__item wide--one-fifth large--one-quarter medium-down--one-half on-sale js-size-filter size-32C / Nude size-32C / Black size-32C / Blue size-34C / Nude size-34C / Black size-34C / Blue size-36C / Nude size-36C / Black size-36C / Blue ">

I then added {% include 'size-filter' %} to the top of my collection.liquid which is located in my templates directory.

The buttons are showing, I am able to click them (even though they are not changing color like how yours is changing to blue), but nothing is happening in regards to filtering the products. I am at a loss and can't figure out what's wrong. I've been tinkering around for days now and have come to a dead end... Can't figure it out! I would really appreciate some guidance so that I can implement this awesome feature into my shop. I'm betting it's a quick 2 second fix and I'm just not savvy enough to figure it out :\

If you'd like to see what I'm talking about, my website is 'www.kittenscloset.com' and the password to view it is 'ilawgh'

Any help is greatly appreciated!

Does this work with pagination?

Hi,

Thanks for sharing such a great work!

My question is will this code work properly when there are more item in the collection than what is shown in the page? Or will it only filter the ones that are displayed on the current page?

Thanks!
Amos

issue for step 4 in debut theme

Hi @jglovier
I was implement your code into debut theme, but i got stuck in step 4. where i attach this code in debut themes?
js-size-filter {% for variant in product.variants %}{% if variant.available %}size-{{ variant.title }} {% endif %}{% endfor %}

Other Variants being included in filter list

It seems that other variant types are being included in the filter list. So, I have a couple of Variants that have color and size, and it's returning both the color+size in the filter options.

Is there any way I can separate out size only as the default list of filters?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.