I'm trying to sort a collection before rendering it in liquid, with the intention of being able to move all sold out products to the bottom, and have them return to their original position once they are back in stock.
I have previously tried filtering the forloop directly, splitting it into two loops with opposing criteria, but the paginate effectively messes it up and renders my logic per page, instead of on the entire collection. That means the sold out products appear at the bottom of each page.
After learning this, I figured my best bet would be to create two collections and combine them before paginating and rendering, and then paginating the collections based on the new, sorted collection. Pagination does not accept arrays, which is where I'm stuck. Here is the code I'm trying to sort the collection with:
{% assign topcol = collection %}
{%- for product in topcol.products -%}
{% assign product = null %}
{%- if product.available or product.tags contains "_boost" -%}
{% assign product = product %}
{%- endif -%}{%- endfor -%}
{% assign bottomcol = collection %}
{% assign product = null %}
{%- for product in bottomcol.products -%}
{%- unless product.available or product.tags contains "_boost" -%}
{% assign product = product %}
{%- endunless -%}{%- endfor -%}
{% assign sortedcol = topcol | concat: bottomcol %}
If I then try
{%- paginate sortedcol.products by section.settings.products_per_page -%}
I get an error "Array 'sortedcol.products' is not paginateable." because I made it into an array instead of retaining the collection structure I believe.
Is there a better way to do this? Am I missing something? A split?