How to Sort Product By Price & Most Frequently Bought Using GraphQL API

ricky7171
Visitor
2 0 0

Hi, I've shopify e-commerce. I want to create a page that displays products with sorting options. I want to sort by title, price, vendor, most frequently bought. So the options will looks like this : 

  • Product Name A-Z
  • Product Name Z-A
  • Price Lowest to highest 
  • Price Highest to lowest 
  • Brand A-Z
  • Brand Z-A
  • Most Frequently Bought

I get data from the graphql API : "/admin/api/2021-04/graphql.json"

So, what I'm asking is how to sort by price ? I've try to make request to graphql API like this :

ricky7171_0-1622446495613.png

 

as you can see that I got an error 

Argument 'sortKey' on Field 'products' has an invalid value (PRICE). Expected type 'ProductSortKeys'.
 
And then I open the productsortkeys documentation in here https://shopify.dev/docs/storefront-api/reference/products/productsortkeys, it said that we can sort by price.
ricky7171_1-1622446602783.png

 

As you can see, I also use the same API version (2021-04)

How come the API is different with the documentation ???

 

And then my second question is, how can I sort product by "Most Frequently Bought" ?

I think that is standard features of e-commerce. 

 

Thank you very much !

Replies 2 (2)

_JB
Shopify Staff
836 100 223

Hey @ricky7171,

The endpoint URL you're calling is for the admin API, but the documentation you linked is from the storefront API. 

The admin API is used by apps that wish to extend functionality, and is usually used by apps or sales channels that will be embedded in the Shopify admin.

The storefront API is used for creating a custom storefront or purchase experience for an individual store.

If you meant to call the storefront API, make sure the URL is in the format of: myshopify.com/api/2021-04/graphql.json

JB | Solutions Engineer @ Shopify 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

ricky7171
Visitor
2 0 0

Hi, thank you for your reply. Ok, if I use storefront API, can I sort product by price ? 

The most important thing here is where I can call an API that can sort products by price

I mean not using liquid (because if I sort by price using liquid, it only applies to 1 page (cannot paginate). And actually it doesn't need to use liquid, I can use javascript myself if only 1 page)

Thank you