I have to make calls to multiple pages of a Woocommerce product database (getting all products in one go seems to not be an option), and the looping and collection of the results isn't working as I expect. I should see an array with just under 900 objects, but all I'm seeing is an empty array. I'm very new to PHP. THe relevant code below:
function get_awesome_products() {
for ($count = 1; $count <= 9; $count++ ) {
if ($count < 2) { $completeProductList = []; }
$baseRequest = 'https://myawesomeapi/wp-json/wc/v3/products/?
consumer_key=xxxx&consumer_secret=xxxx&per_page=100&page=';
$request = wp_remote_get( $baseRequest . (string)$count);
$body = wp_remote_retrieve_body( $request );
$data = array_values(json_decode( $body, true ));
if (count($completeProductList < 1)) {
$completeProductList = $data;
} else {
$completeProductList = array_merge($completeProductList, $data);
}
}
return new WP_REST_Response($completeProductList, 200);
}
add_action('rest_api_init', function() {
register_rest_route('awe/v1', 'aweproducts', array(
'methods' => 'GET',
'callback' => 'get_awesome_products',
'permission_callback' => function () {
return true;
}
));
});