Looping through and combining calls to Woocommerce REST API

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=xxxxconsumer_secret=xxxxper_page=100page=';
 $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;
    }
  ));
});

Topic woocommerce-offtopic rest-api php plugin-development Wordpress

Category Web


@Andrea had at least part of the answer above when he pointed out that I had a variable improperly enclosed above. The other issues at play were:

  • Not calling array_values() on the array after json_decode was called.
  • Variable scope issues?

If anybody else runs into a similar issue, here is the code that worked for me:

function get_awesome_products() {
$baseRequest = 'https://myawesomeapi/wp-json/wc/v3/products/? 
consumer_key=xxxx&consumer_secret=xxxx&per_page=100&page=';
 for ($count = 1; $count <= 9; $count++ ) {
$request = wp_remote_get( $baseRequest . (string)$count);
$body = wp_remote_retrieve_body( $request );
$data = array_values( json_decode( $body, true ));

if ($count < 2) {
  $completeProductList = $data;
} else {
  $completeProductList = array_merge($completeProductList, $data);
}
}
return new WP_REST_Response($completeProductList, 200);

First of all it's not clear why getting all products in one go seems to not be an option if you have control on the API you can add a custom endpoint and write your query to get all the products ( maybe in status "publish") something like :

global $wpdb;
$Q="SELECT * FROM ".$wpdb->prefix."posts WHERE post_type = 'product' AND post_status ='publish'";
$completeProductList = $wpdb->get_results($Q);
return new WP_REST_Response($completeProductList, 200);

should work.

Second : without testing your function I think that

if (count($completeProductList < 1)) should be if (count($completeProductList) < 1) your code is counting a boolean instead of a Countable interface, maybe it's not the final solution but this looks like a bug

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.