Sort products by prices


Would you have some tips and advices about how to let a visitor sorting items by prices with a button, for exemple from lower to higher.

Actuallly, products are diplayed in a products page with pagination, and use some categories and tags.

I thought about a JS solution, but I’m sur it’s not the best way specially with a pagination.

Thank you

  • You can use a simple form with a submit button.
  • You can submit this form via JS still as standard GET request ( page reload)
  • You can send the form request via Ajax

Thanks Texnixe, I was trying something like this:

$items = $page->children()->listed()->sortBy('order', 'asc');
    if(r::is('POST')) {
      $items = $page->children()->listed()->sortBy('price', 'asc');

witch works pretty fine, but does not keep sort order under pagination pages

Yes, it’s better to work with GET request and check for a sort parameter instead of a POST request. That way your pagination will stay intact. If you use a POST request, it get’s more ccomplicated, because you have to store your value in the session to make it available across different pages.

1 Like

It works perfectly with GET, Thank you.