Sunday, May 1, 2022

Override Woocommerce REST api per_page limit to increase the maximum number of products per page

Large queries can hurt site performance, Woocommerce set the default per_page REST parameter at 100 records. Therefore, if you request something higher than 100 you will get this 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)"
            }
        }
    }



 How to Increase per_page Limit in Woocommerce REST API and fix the error:  "per_page must be between 1 (inclusive) and 100 (inclusive)" while requesting products in WooCommerce API

 

ADD THIS CODE TO YOUR THEME functions.php

add_filter('rest_product_collection_params', 'maximum_api_filter');
function maximum_api_filter($query_params) {
    $query_params['per_page']["maximum"]=1000;
    return $query_params;
}