skip to Main Content

I’m trying to find a way for the following:
I’m modifying a liquid template to also include found product variants on a search result page. But when I do the amount of visible products/product variants on a search results varies greatly and leads to bad UX.

We have a pagination break at 28 items. But this only factors in the amount of products it has already displayed. As I now show variants of the products as well, the pages have a very large amount of items on them. If I decrease the pagination limit to – let’s say 4 – I end up with pages that have exactly 4 entries and pages that have let’s say 500 entries depending on the matching variations for a given search term.

This is the overall goal:
Display product variants in the search result page as well as the default products and still have a correct pagination that display a fixed amount of products/variants/search result items per page.

My approach was this:

{% capture total_results %}
    {%- for result in search.results -%}
        {%- case result.object_type -%}
            {%- when 'product' -%}
                // Go over all the variants and match to the search terms
                // Include product-grid-item template in case it matches
            {%- when 'article' -%}
            {%- when 'page' -%}
        {%- endcase -%}
        <!-- Divider: #### -->
    {%- endfor -%}
{% endcapture %}

{% comment %}Break the captured string at the divider string into an array{% endcomment %}
{%- assign total_results = total_resuls | split: "<!-- Divider: #### -->" -%}

<div class="page__description page__description--centered">
    <span>{{ 'search.general.results_count' | t: count: total_results.size, terms: search_terms }}</span>
</div>

{% paginate total_result by 28 %}
    {% for result in total_resuls %}
        {{ result }}
    {% endfor %}
{% endpaginate %}

I get the following liquid error in the frontend: Array 'total_results' is not paginateable.

Is there a way to make it paginateble?

Or is there a way to modify the search.results collection directly so that the pagination doesn’t only consider the amount of results from the shopify search but also the variations?

The reason we do this btw is that shopify does seem to find the products based on an information where only the variation matches. But in this case it only links to the product and the user whould need to go to the correct variation manually again which is very bad UX.

Even if the user searches by ID directly they get linked to the normal product page. And we want them to be linked directly to the correct variant that was the reason the product was included in the search results in the first place.

Any help would be appreciated. Or pointers on how else I could achieve this.

2

Answers


  1. Unfortunately, there’s no way to paginate through your array. paginate tag can only be used with search.results, collection.products on some other predefined objects.

    The way you’re trying to implement it doesn’t seem possible to me. But here are a couple of ideas came to my mind that might help you:

    Option 1. Use search.terms object to build a link to the correct variant. As the main issue is that Shopify search result

    links to the product and the user would need to go to the correct variation manually

    I would just suggest displaying search results as is but apply your logic from

    // Go over all the variants and match to the search terms

    to add a ?variant_id=xxx attribute to the search result URL. Then once customers get to the product page, the variant will be selected based on this query parameter. This logic would also perfectly suit the case when customers search by variant ID.

    Option 2. Build custom search. It would require more efforts and implies not using the Shopify search at all. You would need to sync store products and return the paginated results from your database based on a query from the search form. This option would give you flexibility in displaying your product results.


    I would go with option 1 if the only you want to do is select the correct variant based on the user’s search query. But it may not work if you have multiple matching variants and you either want to display all of them separately or be able to redirect the customer to every matching variant from the results page.

    P.S. Just a hint: you’re using {%- when 'product' -%} block to filter product results only, but you can use ?type=product in your query to search only through products entities, ignoring articles and pages.

    Login or Signup to reply.
  2. Maybe you can try this app https://apps.shopify.com/ultimate-search-and-filter-1 to show variants as separate products on Collection and Search result page without affecting the paginiation in Shopify.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search