How to do custom pagination using woocommerce rest api in my android app?

老子叫甜甜 提交于 2020-03-06 03:10:30

问题


In my android app I am using woocommerce rest api to fetch products from my inventory. As per the documentation "Requests that return multiple items will be paginated to 10 items by default." I need to fetch 25 items per request. As per the documentation I have tried many things but unfortunately I didn't get any success.

For pagination I tried

"mysite/wc-api/v3/products/"+"?page="+pageNum;

Using this I get first 10 products for pageNum = 1, 11-20 i.e next set of 10 items using pageNum = 2 and so on. I need to get 25 items per page.

I tried using

"mysite/wc-api/v3/products?filter[limit]="+25;

but this returns me the first 25 items. Now, how do I get the next set of 25 items i.e. 26 to 50 items.

I can't use

"mysite/wc-api/v3/products?filter[limit]="+50;

as it would give me the first 50 items.

I am referring https://woocommerce.github.io/woocommerce-rest-api-docs/

I just need the endpoints.


回答1:


Finally I figured out the solution myself:

This is if we need to get 25 items for each request.

The endpoint would be like :

"mysite/wc-api/v3/products?filter[limit]=25&filter[offset]="+offset;

This will give 25 items in one go and in each request I need to send the offset i.e. that will be 0,25,50,75..... at each subsequent request.




回答2:


Try this

https://www.[website_name].com/wp-json/wc/v3/products?[consumer_key_here]&[consumer_secret_key_here]&per_page=15&page=1

[website_name] = Your Website Name Here
[consumer_key_here] = Your Consumer Key Here
[consumer_secret_key_here] = Your Consumer Secret Key Here

You can create pagination in an easy way

https://www.website.com/wp-json/wc/v3/products?consumer_key=ckxxxxxxxxxx&consumer_secret=xxxxxxxxxx&page=15&per_page=1

API parameters passed in HTTP Request
per_page = 15
page = 1

You will have to create a variable for page (for page number) and then you will increase page number value by 1 each you time you make a request.

https://www.website.com/wp-json/wc/v3/products?consumer_key=ckxxxxxxxxxx&consumer_secret=xxxxxxxxxx&page=15&per_page=2

This is if we need to get 15 items for each request.

This will get first 15 products on the 1st HTTP request, 15 more products on 2nd request and so on.




回答3:


WooCommerce simply does not support this.

The only way you could do it would be to call page 1 to 3 and only display page 1 - 2 and 5 products of page 3



来源:https://stackoverflow.com/questions/42341185/how-to-do-custom-pagination-using-woocommerce-rest-api-in-my-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!