Custom price filter in Magento

Featured Image

Quite a few people have asked me for a price filter functionality that comes with Magento. Mostly, the questions are same: How does one put the price filter anywhere on the page? How does one set it’s on price ranges in that filter? What defines default price ranges. Is it possible to set price filter for all my products instead of just single category? Lot of questions. The answer might be simpler then you might think.

There are two ways to approach this problem. Head trough wall or stop and think approach.Default Magento price filter works with algoritam which functions on the following logic

  1. Find the lowest and highest price in the range of filtered products.
  2. Find what power of 10 these are in.
  3. If there’s 2 or less ranges available we go one power down.

So one approach would to be to somehow override default price filter logic by rewriting it and embedding some new logic of yours. I call this one head trough wall. I’m not implying that this is wrong approach, on the contrary this is the generic solution. However it does require some more time to come up with.

Second solution is by far more easier. It involves using simple HTML (could use it in your static blocks).

If one wishes to use the price range filter to filter trough entire product collection he can simply create a subcategory under the Default root category and add all of the products to it. Then we write some static html code with urls made to fire up the price filter.

Let’s look at the default Magento sample data. It gies you the following category layout

Root Catalog (6)

  • Furniture (6)
  • Living Room (4)
  • Bed Room (4)

Notice the url link above… It wont work if you put it like http://somesite.domain/furniture/living-room/?price=1%2C199

It wont work like that because by default you do not have price filter functionality covering second subcategory level. What this means is that following link wont work

http://somesite.domain/furniture/living-room/?price=1%2C199

Since the first subcategory level is Furniture, and second are Living Room and Bed Room, by default the following url will work with or without index.php.

  • url: http://somesite.domain/furniture/?price=1%2C199
  • url: http://somesite.domain/index.php/furniture/?price=1%2C199

You could therefore create simple HTML like

<select onchange=”setLocation(this.value)”>
<option value=”http://
somesite.domain/all/?price=1%2C10000″>  <!– Here you place your own ranges –>
0,00 € – 10 000,00 €
</option>
<option value=”http://
somesite.domain/all/?price=2%2C10000″> <!– Here you place your own ranges –>
10 000,00 € – 20 000,00 €
</option>
<option value=”http://
somesite.domain/all/?price=3%2C10000″> <!– Here you place your own ranges –>
20 000,00 € – 30 000,00 €
</option>
.
.
.

</select>

And put it into some of the static block. Notice the links in above HTML code. The /all part of the url is the url key of the All category in which we added all of our products.

Last, but not least is understanding of the “price=…” parametar.

1%2C10000 – where 1 stands for first range of 10000 (last number)
or
2%2C10000 – where 2 stands for second ranfge of 10000, (first would be 0-10000).

So if we were to write something like
3%2C10 it would be the price range from 20-30, where 3 stands for third range of tens (number 10). Firs is from 0-10, second 10-20, third 20-30.

Most of this is quite selfexplanaory so, for those who need this kind of functionality across some special pages, hope you integrate it withouth any problems.

18
Top

Enjoyed this post?

Subscribe to our RSS Feed, Follow us on Twitter and spread it to your friends!

Author

CTO / Magento Certified Developer Plus @inchoo. Father, husband!

Other posts from this author

Discussion 18 Comments

Add Comment
  1. I know that’s a pretty old topic, but I tried this way of doing it and it works pretty well. The only thing is the breadcumb will always be related to the “all” category and not to any other one.
    Do you know a way to take another category?

  2. You simply copy – paste – modify link according to the one you get when using real price filter for some category.

  3. Marcio

    How to enable Price Filter ?

    Im my Site not enabled filter in Category !

    http://portal.cerebrum.com.br/magento/cerebrum/index.php/brindes-promocionais.html

  4. Not working for all categories

  5. Can we put magento’s default filter on product view page?
    If yes then how can we?
    Thanks in advance

  6. Ali Nasrullah

    i have made a custom attribute and i want to filter my complete store with it is it possible i.e

    i have devices for example w995i user select its device when it hits my site and all categories will be filtered and displays only those products whose attribute value is set to w995i .. can this be possible..

    i need it…please

  7. cyberscripts
  8. Surya

    this ans is good, but when we want to select products filter by price or any attribute of all catagoty what we will do.

    please give me replay

  9. AA

    Hey Branko,

    Thanks for the great post.

    I am starting out with magento so pls excuse my banality.
    Is there a way to create a customer filter from the front end?

    Thanks in advance,
    Amit

  10. Tahir Y

    Thank you so much for the tutorial, can you please guide me how to add a filter for “Best Sellers” and “New Arrivals” on product listing page’s left sidebar?

    I mean to say I want to add two links on product listing page’s left sidebar.

    - Best Sellers
    - New Arrivals

    When clicked “Best Sellers” only the best selling products from that category will be listed and when “New Arrivals” link clicked products will sorted by data DESC.

    any help will be highly appreciated.

    Thanks

  11. Does anybody know where to get a price filter where I can put in 2 different price ranges?

  12. surya

    i default magento filter works on a category at a time. But i want
    when i click on price filter it shows all filtered product of all categories.

  13. Santhosh

    I am facing a problem in magento layered navigation.I want price filter like as given below:

    Under $38 (5)

    $38 – $43(5)

    $44 – $48(3)

    Larger than $48 (3)
    how to do this?

    Thank You

  14. surya

    Hi Santhosh,

    You can custmize price filter as you want.

    Check the following link

    This will solve your problem…

    http://www.codegyan.com/2012/01/06/customize-filter-price-range-in-magento/

    I hope this will help you.

    Thanks

    surya

  15. Santhosh

    Hi Surya,

    Thank you so much for the link

    ~
    Santhosh

  16. kapil

    hi
    i want to customise price filter.
    i just want two filter options
    first one is lower then $2000
    and second one is upper then $2000

    So can you please help me for this

    Regards

  17. Purvi123

    how to add drop down in price in advanced search in magento

  18. Rishabh

    Hi..

    I want to add price filter slider in product page on the right side.

    Pl. tell me how can i do this.

    Thanks
    Rishabh

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top