easythemestore

How to Extend WooCommerce REST API for Custom Apps

How to Extend WooCommerce REST API for Custom Apps

Extending the WooCommerce REST API allows you to connect custom apps, mobile applications, or third-party services with your WooCommerce store. This guide covers creating custom endpoints, modifying responses, and securing your API for tailored e-commerce solutions.


🔧 Prerequisites

  • WooCommerce installed (v4.5+ for best compatibility)
  • Basic PHP & WordPress REST API knowledge
  • A development environment (Local by FlyWheel, XAMPP, etc.)
  • Authentication method (API keys, JWT, or OAuth). Our YouTube channel; https://www.youtube.com/@easythemestore

🚀 Method 1: Register Custom Endpoints

Step 1: Create a Plugin

Add this to wp-content/plugins/woocommerce-custom-api/woocommerce-custom-api.php:

<?php
/**
 * Plugin Name: WooCommerce Custom API
 * Description: Extends WooCommerce REST API for custom apps.
 * Version: 1.0
 */

if (!defined('ABSPATH')) exit;

add_action('rest_api_init', 'register_custom_woocommerce_endpoints');

function register_custom_woocommerce_endpoints() {
    register_rest_route('wc/v3', '/custom-products', [
        'methods' => 'GET',
        'callback' => 'get_custom_products',
        'permission_callback' => '__return_true' // Open endpoint (secure in production!)
    ]);
}

function get_custom_products($request) {
    $args = [
        'status' => 'publish',
        'limit' => 10,
    ];
    $products = wc_get_products($args);
    return array_map(function($product) {
        return [
            'id' => $product->get_id(),
            'name' => $product->get_name(),
            'price' => $product->get_price(),
            'custom_field' => get_post_meta($product->get_id(), '_custom_field', true)
        ];
    }, $products);
}

🔹 What This Does:

  • Creates a new /wc/v3/custom-products endpoint.
  • Returns 10 published products with a custom meta field.

🔐 Method 2: Modify Existing API Responses

Add Custom Fields to Product Responses

add_filter('woocommerce_rest_prepare_product_object', 'add_custom_data_to_api_response', 10, 3);

function add_custom_data_to_api_response($response, $product, $request) {
    $response->data['custom_field'] = get_post_meta($product->get_id(), '_custom_field', true);
    return $response;
}

🔹 Usage:

  • Now, all /wc/v3/products responses include custom_field.


🔒 Method 3: Secure Custom Endpoints

Option A: WooCommerce API Keys (Recommended)

  1. Go to WooCommerce → Settings → Advanced → REST API.
  2. Click Add Key, generate Consumer Key & Secret.
  3. Authenticate requests with:
    curl –user “ck_xxx:cs_xxx” https://yoursite.com/wp-json/wc/v3/custom-products

Option B: JWT Authentication (For Mobile Apps)

Install JWT Authentication for WP REST API and modify permissions:

'permission_callback' => function() {
    return current_user_can('edit_products'); // Only admins/editors
}

⚡ Advanced Customizations

1. Create a POST Endpoint (Add Custom Data)

register_rest_route('wc/v3', '/submit-order', [
    'methods' => 'POST',
    'callback' => 'handle_custom_order_submission',
    'permission_callback' => '__return_true'
]);

function handle_custom_order_submission($request) {
    $params = $request->get_params();
    $order = wc_create_order();
    $order->add_product(wc_get_product($params['product_id']));
    $order->set_customer_id($params['user_id']);
    $order->save();
    return ['status' => 'success', 'order_id' => $order->get_id()];
}

2. Extend WC REST API Class (For Complex Logic)

class WC_Custom_API_Controller extends WC_REST_Controller {
    public function register_routes() {
        register_rest_route('wc/v3', '/custom-endpoint', [
            'methods' => 'GET',
            'callback' => [$this, 'get_custom_data'],
        ]);
    }
    public function get_custom_data() {
        return ['message' => 'Custom API Working!'];
    }
}
add_action('rest_api_init', function() { (new WC_Custom_API_Controller())->register_routes(); });

🔌 Testing Your API

  1. Using Postman:

    • Send a GET request to:
      https://yoursite.com/wp-json/wc/v3/custom-products

  2. Via cURL:
    curl -X GET https://yoursite.com/wp-json/wc/v3/custom-products


🚨 Common Issues & Fixes

ProblemSolution
403 ForbiddenCheck API keys or JWT tokens
404 Not FoundEnsure permalinks are saved (Settings → Permalinks → Save)
Missing DataVerify register_rest_route namespace matches (wc/v3)
Slow ResponsesOptimize queries with 'posts_per_page' limits

📚 Recommended Plugins

  1. WP REST API Controller – Expose custom fields.
  2. JWT Authentication – Secure endpoints.
  3. ACF to REST API – Add ACF fields to responses.

🚀 Final Thoughts

Extending WooCommerce’s REST API lets you:
✔ Build mobile apps for your store
✔ Sync data with ERPs/CRMs
✔ Create custom checkout flows
✔ Automate order processing

Start with simple endpoints, then expand as needed. Always secure production APIs with authentication!

Which API extension will you build first? 🛒💻