I have this code on a Woocommerce site, which successfully returns the entire responded object:
function checkProducts($data) {
$product_ID = $data['id'];
$api_response = wp_remote_post( 'https://whatever.net/wc-api/v3/products/'.$product_ID, array(
'method' => 'GET',
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'key:secret' )
)
) );
$body = json_decode( $api_response['body'] );
if( wp_remote_retrieve_response_message( $api_response ) === 'OK' ) {
return $body;
}
}
I have tried to only return the stock_quantity
property in many ways, which is present on the object, with no success, among others:
$body = json_decode( $api_response['body']['stock_quantity'] );
return $body['stock_quantity']:
return $body.stock_quantity;
JSON returned:
{
product: {
id: 687,
created_at: "2019-10-08T10:21:57Z",
updated_at: "2019-10-08T13:53:32Z",
type: "simple",
status: "publish",
downloadable: false,
virtual: false,
permalink: "https://germanalvarez.net/develop/ww/producto/tww-fin-de-semana-del-3-al-4/",
sku: "",
price: "200",
stock_quantity: "200",
sale_price: "120",
...
How could I archive this? Thank you