Can we sort store search results?

GD
New Member
19 0 0

I know we can specify 'product', 'article' or 'page' to narrow our search results. But, is there some way to sort within one of those?

For example, if I set the shopify store search form to only return 'products', can I sort them by product type, or anything else?

0 Likes
Subhranil
Shopify Partner
39 8 49

Use sort option 'price' or 'title'. That is,

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

or

{% assign search_products = search.results | sort:'title' %}

So, the code looks like:

{% assign search_products = search.results | sort:'title' %}
{% for item in search_products %}
<!-- loop over each result -->
{% endfor %}
IoEuropa
Tourist
5 0 2

The will sort by price ... in a manner of speaking.

Unfortunately, if you sort by price, you'll get clumps of articles, then clumps of parts that are sorted by price, then some more articles, then more parts sorted by price and so on. This is what happens when I use this code.

So needless to say, this code will not sort by 'price' correctly if you want to include 'blog articles' and 'pages' in the search.

 

0 Likes
HuskOfficial
New Member
1 0 0

Thanks for your great reply.  We wish to sort our search results from highest to lowest price.  Our search.liquid file currently looks like this.  Could you advise where we would insert your code to effect this? Kind regards, Damian

 

{% paginate search.results by 40 %}

<div class="page-width page-content">

{%- include 'breadcrumbs' -%}

<div class="grid">
<div class="grid__item">
<header class="section-header">
<h1 class="section-header__title">
{{ 'general.search.title' | t }}
</h1>
</header>

{% include 'search-bar', search_btn_style: 'btn', search_bar_location: 'search-bar--page' %}

{% if search.performed %}

<hr class="hr--medium">

<div data-section-id="{{ section.id }}" data-section-type="collection-template">
<div class="section-header">
<h2 class="section-header__title">
{% comment %}
{{ 'general.search.result_count' | t: count: search.results_count }}
{% endcomment %}
</h2>
{% if search.results_count == 0 %}
<p>
{{ 'general.search.no_results_html' | t: terms: search.terms | replace: '*', '' }}
</p>
{% endif %}
</div>

<div class="grid grid--uniform">
{% for item in search.results %}
{% if item.object_type == 'product' and item.available == true %}
{% if item.object_type == 'product' %}
{%- assign product = item -%}
{% include 'product-grid-item' %}
{% else %}
<div class="grid__item grid-search medium-up--one-quarter small--one-half">
<div class="grid-search__page">
<a href="{{ item.url }}" class="grid-search__page-link">
<span class="grid-search__page-content">
<span class="h4">{{ item.title }}</span>
{% if item.object_type == 'article' and item.image %}
{%- assign img_url = item.image | img_url: '1x1' | replace: '_1x1.', '_{width}x.' -%}
<img class="grid-product__image lazyload"
data-src="{{ img_url }}"
data-widths="[180, 360, 540]"
data-aspectratio="{{ item.image.aspect_ratio }}"
data-sizes="auto"
alt="{{ item.title | escape }}">
{% endif %}
{{ item.content | strip_html | truncatewords: 45 }}
</span>
</a>
</div>
</div>
{% endif %}
{% endif %}
{% endfor %}

{% for item in search.results %}
{% if item.object_type == 'product' and item.available == true %}
{% if item.object_type == 'product' %}
{% if settings.quick_shop_enable %}
{%- assign product = item -%}
{% include 'quick-shop-modal' %}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
</div>
</div>

{% if paginate.pages > 1 %}
{% include 'pagination' %}
{% endif %}

{% endif %}
</div>
</div>
</div>

{% endpaginate %}

0 Likes