Sarathlal N

Customize / Modify WooCommerce REST API response using filter

In some WooCommerce customization projects, I need to modify the REST API response for order, product & customer request. The filter functions for these 3 requests are given below.

Modify response on get customer request

curl https://example.com/wp-json/wc/v3/customers/<user_id> \
	-u consumer_key:consumer_secret

Filter function

add_filter('woocommerce_rest_prepare_customer', 'filter_response', 10, 3);
function filter_response($response, $user_data, $request){
	// Customize response data here
	$user_id = $user_data->ID;
	
	$response->data["custom"] = 3;
	return $response;
}

Modify response on get product request

curl https://example.com/wp-json/wc/v3/products/<product_id> \
	-u consumer_key:consumer_secret

Filter function

add_filter('woocommerce_rest_prepare_product_object', 'filter_product_response', 10, 3);
function filter_product_response($response, $post, $request){
	// Customize response data here
	$product_id = $post->get_id();
	
	$response->data["custom"] = 12;
	return $response;
}

Modify response on get order request

curl https://example.com/wp-json/wc/v3/orders/<order_id> \
	-u consumer_key:consumer_secret

Filter function

add_filter('woocommerce_rest_prepare_shop_order_object', 'filter_order_response', 10, 3);
function filter_($response, $post, $request){
    // Customize response data here
    $order_id = $post->get_id();

    $response->data["custom"] = 12;
    return $response;
}

Got a project in mind? Send me a quick message, and I'll get back to you within 24 hours!.

Recent Posts

  1. Disabling Payment Methods in WooCommerce Based on Conditions
  2. How to Update Product Quantity in WooCommerce Using Custom Code
  3. Dynamically Generating a Table of Contents in WordPress
  4. Direct Checkout in WooCommerce - Add Product to Cart from Checkout Page & Skip Shop, Product, and Cart Pages
  5. Understanding the Impact of git reset --hard Command

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.