woocommerce / woocommerce-rest-api-js-lib

New JavaScript library for WooCommerce REST API
https://www.npmjs.com/package/@woocommerce/woocommerce-rest-api
MIT License
282 stars 75 forks source link

Order by Price? #48

Open sajjadalis opened 4 years ago

sajjadalis commented 4 years ago

How to get items Ordered by Price?

I found this. https://www.storeurl.com/wc-api/v3/products?filter[order]=asc&filter[orderby]=meta_value_num&filter[orderby_meta_key]=_regular_price

But not sure how to use filter inside params object:

api.get("products", {
  per_page: 20
})

Thanks

climba03003 commented 4 years ago

You should check the API Docs for more information. Here

Example Code:

api.get("products", {
  order: "asc", // Order sort attribute ascending
  orderby: "date" // Order by Date
})

If you want a custom order by value. You should create a filter for rest_event_query and just pass the corresponding parameters you have added in the params object. You may be interested in WordPress REST API order by meta_value or WordPress REST API post orderby custom value.

sajjadalis commented 4 years ago

I have checked API Docs and there is no option for order by price.

Then i found this topic on WordPress.org. https://wordpress.org/support/topic/how-to-sort-products-by-price-in-rest-api-v3/

So i was wondering how to pass those filter[orderby]=meta_value_num & filter[orderby_meta_key]=_regular_price inside that parameters object.

Thanks for the links. I will look into that.

climba03003 commented 4 years ago

I don't know whether filter is a valid parameter for the REST API. If you really want to pass those value in the param object.

Example code:

api.get('products', {
  "filter[orderby]": "meta_value_num",
  "filter[orderby_meta_key]": "_regular_price"
})
sajjadalis commented 4 years ago

Thanks again for the help. I tried this but it's not working. I think filter is not a valid parameter for REST API.

It would be great if there is a way to get it done. Order by Price should be a parameter as it's pretty common way of sorting products on any store site.

seb86 commented 4 years ago

CoCart Products API let's you sort products by price @sajjadalis @climba03003