Gagandeep
Gagandeep

Reputation: 69

Get All Categories from Woocommerce using REST API

I am trying to retrieve all categories using Woocommerce rest API. here is my URL

https://wapi.sriyagcommerce.com//wp-json/wc/v2/products/categories?consumer_key=ck&consumer_secret=cs&per_page=100

while using above url I am getting Error.

code    "rest_invalid_param"
message "Invalid parameter(s): per_page"
data    
status  400
params  
per_page    "per_page must be between 1 (inclusive) and 100 (inclusive)"

I want to increase per page limit. I already have done with products by using below code and want to do the same with categories

function maximum_api_filter($query_params) {
    $query_params['per_page']["maximum"]=100000;
    return $query_params;
}

add_filter('rest_product_collection_params', 'maximum_api_filter');

Upvotes: 2

Views: 7250

Answers (2)

Md Tofazzal Hossain
Md Tofazzal Hossain

Reputation: 216

You couldn't increase the list size. The maximum limit in Woocommerce is 100.

You can specify pages with the page parameter

https://www.yourcompany.com/wp-json/wc/v2/products?per_page=100&page=2

After the first API call, you can find the total number of pages and records from the

HTTP headers X-WP-TotalPages and X-WP-Total .

After getting the total page from X-WP-TotalPages, I fetch all the records using the bellow approach. \

int totalPage = 1;
int currentPage = 0;
while(totalPage > 0) {
    currentPage =  currentPage + 1;
    String url = "https://www.yourcompany.com/wp-json/wc/v2/products?per_page=100&page=" + currentPage;
    ResponseEntity<DTO> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, DTO.class);
    totalPage = responseEntity.getBody().X-WP-TotalPages;
    if(currentPage == totalPage){
        totalPage = 0;
    }
}

Upvotes: 2

Azurry
Azurry

Reputation: 526

I must've commented very early in the morning

There is NO Way of increasing the size per page past a 100 Its a limit set by the API team you cannot exceed a 100.

Now I do not know what Your using to develop, But i'd consider using a Collection and just pull the first 100, Index the Page pull a 100 Combine Collections and so on.

But as for Getting all 300 in one instance from one API call that cannot be done sadly.

Upvotes: 2

Related Questions