WooCommerce REST API: read orders and update stock from another system
WooCommerce has its own REST API under /wp-json/wc/v3/: products, orders, customers, coupons, shipping zones and more. An ERP, a warehouse system or a script on another server can read new orders and update stock through it, without any plugin on the shop. Unlike most of the core WordPress API, every WooCommerce endpoint requires authentication, also for reading.
Problem: A warehouse system must fetch new paid orders every 15 minutes and send the current stock of each product back to the shop.
Solution: Create API keys in WooCommerce → Settings → Advanced → REST API (read/write, for a dedicated user), call GET /wc/v3/orders with status and after, and PUT /wc/v3/products/{id} with stock_quantity. Authenticate with the consumer key and secret over HTTPS.
A small client
The example runs inside another WordPress site, so it uses the WordPress HTTP API; the same requests work from any language.
<?php
class HA_WC_Client {
private $base;
private $auth;
public function __construct( $store_url, $consumer_key, $consumer_secret ) {
$this->base = rtrim( $store_url, '/' ) . '/wp-json/wc/v3';
$this->auth = 'Basic ' . base64_encode( $consumer_key . ':' . $consumer_secret );
}
private function request( $method, $path, $body = null ) {
$args = array(
'method' => $method,
'timeout' => 30,
'headers' => array( 'Authorization' => $this->auth, 'Content-Type' => 'application/json' ),
);
if ( null !== $body ) {
$args['body'] = wp_json_encode( $body );
}
$response = wp_remote_request( $this->base . $path, $args );
if ( is_wp_error( $response ) ) {
return $response;
}
$code = wp_remote_retrieve_response_code( $response );
$data = json_decode( wp_remote_retrieve_body( $response ), true );
if ( $code >= 400 ) {
return new WP_Error( isset( $data['code'] ) ? $data['code'] : 'wc_api_error', isset( $data['message'] ) ? $data['message'] : 'HTTP ' . $code );
}
return $data;
}
public function get_orders( $after, $status = 'processing', $page = 1 ) {
return $this->request( 'GET', '/orders?' . http_build_query( array(
'status' => $status,
'after' => $after, // ISO 8601, e.g. 2020-11-30T00:00:00
'per_page' => 100,
'page' => $page,
'orderby' => 'date',
'order' => 'asc',
) ) );
}
public function update_stock( $product_id, $quantity ) {
return $this->request( 'PUT', '/products/' . (int) $product_id, array(
'manage_stock' => true,
'stock_quantity' => (int) $quantity,
) );
}
}
$shop = new HA_WC_Client( 'https://shop.example.com', HA_WC_KEY, HA_WC_SECRET );
$orders = $shop->get_orders( '2020-11-30T00:00:00' );
if ( ! is_wp_error( $orders ) ) {
foreach ( $orders as $order ) {
// $order['id'], $order['line_items'], $order['billing'], $order['shipping']...
}
}
Keep the keys in constants or environment variables on the calling system, not in the code.
Details that matter
- Pagination.
per_pageis limited to 100. The total number of pages is in theX-WP-TotalPagesresponse header; request the next pages until you reach it. - Variations. The stock of a variable product is set per variation:
PUT /products/{product_id}/variations/{variation_id}. - Many products.
POST /products/batchwith anupdatearray changes up to 100 products in one request, much faster than one request per product. - Keys on plain HTTP. Without HTTPS, WooCommerce expects OAuth 1.0a signatures instead of Basic auth, because the secret would otherwise travel in clear text. A shop that handles orders should use HTTPS anyway.
Some hosts strip the Authorization header before PHP sees it, and every request then fails with 401. Passing the keys as query parameters (?consumer_key=...&consumer_secret=...) over HTTPS works around it; the better fix is the host passing the header through.
NOTE: Polling every 15 minutes is simple and robust. For near real-time updates, WooCommerce webhooks (Settings → Advanced → Webhooks) send an order to your URL as soon as it is created or updated; keep the polling as a fallback for webhooks that fail.