How to customize Woocommerce REST API response

Jeong Grey
3 min readSep 24, 2021

--

Introduction

In this article, I will show you how to customize Woocommerce REST API response. You can also save time by customizing a well-made REST API for your 3rd party application. It’s pretty simple so just read and do the following instructions.

Woocommerce REST API

Woocommerce is the most popular e-commerce platform these days.
It also supports REST API for 3rd party applications.
I have an android application project using Woocommerce REST API as backend. I have to customize Woocommerce REST API because there are tons of unnecessary information and missing ones.

Understanding Filter Hook

You have to use Filter Hooks to customize REST API.
Thanks to developers, filter hooks are applied to each part of the API.
If you understand what hooks are used, you can customize the response.
Unfortunately, REST API hooks are not documented well so you have to find them. I will let you know how to do it.

You can find the source code of Woocommerce REST API here.
Each part has its own controller where you can find a filter hook for customization.
Firstly, find the right controller you want to customize.
If you want to customize customer information? Find customer controller.
product? Go to product controller.

How filter hooks work

In the controller source code, you have to find the function that generating the response. Usually, it has the name kind of prepare_something. You can inspect the code. Before it generates a return value, there is a filter in the function. If you find the filter then the game ends!

Examples

I will show two examples for you to deeply understand the whole process.
The first example shows how to customize customer information.
What you have to do is find the filter hook defined in the customer controller.

Woocommerce REST API Customer Controller

This function created a response to customer retrieve API.
It is written on the comment so it’s not that difficult to find it.
The filter is defined at the end of the function logic.
woocommerce_rest_prepare_customer
Following values will be passed to the callback function as parameters.

Now, you can customize the API.
It’s the same when you develop a WordPress plugin.
Create a directory in plugins and make a file with the same name.

Add filter hook you find before.
Remember you have to recognize parameters passed from the filter hook.
You might need them when you implement the callback function.
You can customize response data and return it.
It’s done :)

How about customizing product information?
It’s the same process. You have to go product controller and find out which filter hook is applied in the function.

Woocommerce REST API Product Controller

This function finds the product and creates return data.
You can find the filter at the end of the function.

woocommerce_rest_prepare_($this->post_type)_object
post_type means product in this case.
Now, you can customize the product retrieve API.

It’s pretty easy if you know what filter is applied in the part you want to customize.
I hope this article helps you to use Woocommerce REST API more useful.
Don’t forget to clap if it gives value to you.

--

--

Jeong Grey
Jeong Grey

Written by Jeong Grey

Dev + Marketing = Enterpreneur

Responses (2)