0

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;
    }
  ));
});

2 Answers 2

0

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

2
  • Sorry, I should have specified, while I have read access to this API, I don't have control over the API, and Woocommerce's current REST API doesn't have a way to request all of whatever in the current version.
    – Michael C
    Commented Feb 18, 2020 at 15:18
  • Did you try to change the count() as per second part of my answer? Commented Feb 18, 2020 at 15:46
0

@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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.