Updating stock via Woocommerce API V2

I’m trying to update my woocommerce shop’s stock levels via the API interface. I’m using the kloon/WooCommerce-REST-API-Client-Library but unfortunately it only seems to read product information and doesn’t allow me to PUT stock info.

Reading the API docs I can see I can update a product using the following:

Read More
curl -X PUT https://example.com/wc-api/v2/products/546 
    -u consumer_key:consumer_secret 
    -H "Content-Type: application/json" 
    -d '{
  "product": {
    "regular_price": "24.54"
  }
}'

But non of the function in the API client library access the PUT features of product, I have found a modified client library for the V2 api rodolfojnn/WooCommerce-REST-API-Client-Library which has updated PUT functions:

/**
* Update a product by id
* @param int $product_id
* @param array $data
* @param string $method
* @return mixed|json string
*/
    public function update_product($product_id, $data, $method = "PUT") {

        return $this->_make_api_call('products/' . $product_id, ['product' => $data], $method);
}

But this now throws an Parse error: syntax error, unexpected ‘[‘ error because of the [‘product’ => $data] bit – I tried converting it to an array but that also errors, anyone any idea why (I’m running this in codeigniter btw)

Related posts

Leave a Reply

1 comment

  1. try modifying

    public function update_product($product_id, $data, $method = "PUT") {
    
        return $this->_make_api_call('products/' . $product_id, $data, $method);
    }
    

    Pass only $data as 2nd parameter to update_product

    $data=  json_encode(
    
        array( 'product' =>
    
           array( 
              'regular_price'        => "10.26",
              'managing_stock'   => true,
              'in_stock'         => true,
              'stock_quantity'   => 45
           )
        )
    );