WooCommerce REST API - Get all products with its variations details in a single api request

I am new to woo REST API, i have a php page where i need to populate all products and variations. so i am using curl then im looping it and passing product id to get variations so inside loop im using but it takes more time load because each product have more variations so is there any way get all products and variations in a single request?

like: is there any customisation can be made in class-wc-rest-products-controller.php or in any query or any where in woocommerce? so that i can get the result.

mycode:

<?php
$curl = curl_init();
curl_setopt_array($curl, array( CURLOPT_URL => "", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_SSL_VERIFYPEER=> false, CURLOPT_HTTPHEADER => array( "Authorization: Basic *****************", "Cache-Control: no-cache" ),
));
$response = curl_exec( $curl) ;
curl_close( $curl );
$Product = json_decode( $response );
//after getting all product. looping all to get variations price.
foreach ( $Product as $row ){ $product_id= $row->id; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "".$product_id."/variations/", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_SSL_VERIFYPEER=> false, CURLOPT_HTTPHEADER => array( "Authorization: Basic **************", "Cache-Control: no-cache" ), )); $response1 = curl_exec( $curl ); curl_close( $curl );
}
$variations=json_decode( $response1 );
print_r( $variations->price );
// here getting variation of particular product 

so when we call we get all product and only variations id belongs to it as

 "variations": [ 603, 604, 605, 606 ], 

but what i need is details(price) of each variations

 { "id": 603, "date_created": "2018-06-18T12:45:58", "date_created_gmt": "2018-06-18T12:45:58", "date_modified": "2018-06-29T06:41:21", "date_modified_gmt": "2018-06-29T06:41:21", "description": "", "sku": "", "price": "550", "regular_price": "550",
}

note: i am creating a separate test.php page in /var/www/html/ . this page is not inside any wp-content

2 Answers

I did this using their phpLib

while (count($woocommerce->get('products',array('per_page' => 100, 'page' => $page))) > 0) { $all_products = array_merge($all_products,$woocommerce->get('products',array('per_page' => 100, 'page' => $page))); $page++; } if ($source_product->type = "variable") { $variation = $woocommerce->get('products/'.$source_product->id.'/variations'); foreach ($variation as $source_child) { //do stuff }
}
2

every time in a loop you rewrite the value - response1

if you want save all value you need add this value in array for example:

<?php
$array = array();
foreach ($Product as $row){ $product_id= $row->id; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "" . $product_id . "/variations/", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 30, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_SSL_VERIFYPEER=> false, CURLOPT_HTTPHEADER => array( "Authorization: Basic **************", "Cache-Control: no-cache" ), )); $array[] = json_decode( curl_exec( $curl ) ); curl_close( $curl );
}
print_r( $array );
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like