How can I sort collections by product availability in descending order?

Hello,

I wanted to check the possibility to create a new sorting method to sort products of current collection by the stock availability.

In example, the customer could sort collection page by product availability DESC.

I have search around the Web to find some starting point codes but it looks like nothing has really been done without a paid App or some sort of third party script.

I am checking in the collection.sort_options (https://shopify.dev/api/liquid/objects/collection#collection-sort_options) object to see if I can add something.

Writing this tread as a starting point and to post my researches or code if I came up with something nice!

If you have hints or documentation to read, feel free to share.

Hi @poliquinp ,

Yes. As far as I know, Shopify does not support sort by product quantity by theme code or attributes.

Therefore, the app is the most important thing for it.

@LitCommerce could your give me more information about your app and how you could potentially fix this issue ?

Thread’s a few years old and never got resolved, so posting the answer for anyone landing here from search.

collection.sort_options is read-only. You can read the list, but you can’t add to it. The storefront dropdown works by setting a ?sort_by= parameter, and Shopify only honours a fixed set of values — manual, best-selling, title-ascending, title-descending, price-ascending, price-descending, created-ascending, created-descending. Add your own <option> and it renders perfectly and does nothing, because Liquid resolves the product order server-side before your markup runs.

That’s also why the freakdesign snippet feels awkward to integrate. It re-sorts the products Liquid has already handed you — which is one paginated page, not the collection. On a 200-product collection paginated at 24, you’d be sorting page one’s 24 products among themselves while the in-stock items on page eight stay on page eight. Liquid caps collection.products at 50 anyway, so there’s no way to pull the whole collection and sort it properly. That approach works for small single-page collections and breaks everywhere else.

Two things do work.

For the default order: set the collection’s sort order to Manual and have something write the availability-ordered sequence back through the Admin API on a schedule. Nothing in your theme changes — the products simply arrive in the order you decided. Worth saying that most shoppers never touch the sort dropdown, so this alone usually gets you most of what you’re after.

For a customer-selectable option: there isn’t a clean route. The nearest workaround is a second collection holding the same products ordered by availability, with your extra dropdown option navigating to that collection’s URL instead of setting a sort_by param. It works, but you’re maintaining two collections and will want canonical tags so they don’t compete in search.

One merchandising note, because “availability DESC” can mean two quite different things. Sorting by total inventory quantity usually isn’t what people actually want — a product with 500 units of XXL and nothing else scores high and is unbuyable for most shoppers. The useful version is normally “products where the sizes that carry demand are in stock come first,” which is a grouping condition rather than a numeric sort. Worth defining it that way before building anything.

Disclosure: I’m at Seventh Triangle and we build Every Possible Sort ( Every Possible Collection Sort - Auto-sort collections by bestsellers, revenue, stock and price | Shopify App Store ), which handles the Manual-order half including variant-level availability conditions. The read-only sort_options part is just how the platform works, though, whatever you end up using.