Sort the search results based on price

Hi,

We are using the minimal theme. We would like to sort the search results based on price. How could we acieve this? I was looking to tweak the code that fetches search result and sort it there. But I am unable to find where exactly does the search fucntion is performed.

Ater performing search I am seeing the results in  {% search.result %} in search.liquid, but I cant figure out where search is happening in the fisrt place so I could tweak to sort the results before it gets to search page for display.

Is there any way to sort search results?


Shopify Designer and Developer, Dubai, UAE

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Click Accept as Solution
0 Likes
Shopify Staff
Shopify Staff
657 35 76

Hi there!

Don here from Shopify. :)

I've checked in with our theme specialists here who have a possible customisation here you can try.

This would work slightly differently, depending on the specifics of your theme; though you can try this out and if it doesn't work for you, you can give us a link to your store here and we can check it out.

Once you have found your search results in the code (as you seem to have already done), likely in the search.liquid file or equivalent, you can add the following:

{% assign sorted = search_results | sort: 'price' %}
{% assign reverse_sorted = sorted | reverse %}

{% for item in sorted %}
..loop code
{% endfor %}


Check it out and let us know if this works for you, if not then just give us a link to your store here so we can take another look at this! 


Regards,
Don

Don | Social Care @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

0 Likes

Hi Don,

Thank You for your help. But I wasn't able to figure it out. 

microshop-ae.myshopify.com is the site address. Could you please have a look into it?

 

 


Shopify Designer and Developer, Dubai, UAE

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Click Accept as Solution
0 Likes
Shopify Staff
Shopify Staff
657 35 76

Hi again!

Don from Shopify once more. :)

Thanks for that link.

You should be able to put this in place by following these steps:

In the code editor in Template > `search.liquid`

Add this code to line 51, above where it says `{% if grid_results == false %}`

{% comment %} sorts results by price from low to high {% endcomment %}
{% assign sorted = search.results | sort: 'price' %}

{% comment %} remove comments below if you want to sort results by price from high to low {% endcomment %}
{% comment %}{% raw %}
{% assign sorted = sorted | reverse %}
{% endraw %}{% endcomment %}

The second line is commented out and you can uncomment it if you want to reverse the sorting.

Then, replace the variable `search.results` with `sorted` on lines 61 and 69
One thing to bear in mind, however, is that this only affects the display of the *current set* of results on the page, not all the results in the search.

We cannot alter the search functionality as that is part of Shopify core, so if you wish to do this for all products, you can look into adding a search app from our app store to expand on the inbuilt functionality.

All the best!


Regards,
Don

Don | Social Care @ Shopify
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit the Shopify Help Center or the Shopify Blog

0 Likes

Hi Don,

It did not work. What would hapoen is that the search page would not show any items. Howver, there will be pagination. Which means, it is searching but our code to display products does not work. 


Shopify Designer and Developer, Dubai, UAE

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Click Accept as Solution
0 Likes
New Member
1 0 1

Hi! I was struggling with the same issue and my friend and I found a solution. We are using Turbo theme (is a custom theme) and in the file search-template.liquid, line 187 (for our theme, maybe yours is different, if you can't find it use the search) you will see something like this:

 {% assign products = search.results %}

We added the "sort" and it have to look like this:

 {% assign products = search.results | sort: 'price' %}

Hope it helps! I attached a screenshot :)

1 Like
Shopify Expert
9807 92 1560

Just keep in mind that ^ will only sort a single paginated set - and not the entire results.

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
Shopify Expert
6 0 0

Json, Is there any way to sort entire resultset?

 

any JQuery or anything, Please help

0 Likes
Shopify Expert
6 0 0

Jason, As you said above code sort only one page, Kindly provide me solution for the entire resultset sorting? How can I do it?
Can it be done by scrips/jquery/restAPI - please tell me

0 Likes
Tourist
3 0 1

Here we are in 2019, and developing on Shopify is  still an absolute pain. It's truly mind-boggling how those extremely basic things aren't implemented. I cannot comprehend how people build complex stores with this product.

0 Likes