sort shopify products by availability

Feb 23, 2024
1 minute to read

I ran into a very basic issue and was surprised that Shopify didn’t have a built-in, straightforward solution for it. Let’s say you’d love to sort your “out of stock” products towards the end of the list, so you hop over to your theme’s code editor and slap a good ol’ sort filter ontop of your products array, which sounds very fitting. But it seems Spotify can’t figure out how to sort boolean properties, so it leaves the array unchanged.

{% assign sorted_products = collection.products | sort: 'available' %}

{% for product in sorted_products %}
  ... 👎

But we can luckily use the where filter instead to achieve this, coupled with concat. We’ll create an array with our products and one without and then smoosh them together.

{% assign available_products = collection.products | where: 'available' %}
{% assign out_of_stock_products = collection.products | where: 'available', false %}
{% assign products = available_products | concat: out_of_stock_products %}

{% for product in products %}
  ... 👍