Extend the WooCommerce API

The WordPress/WooCommerce API is a fantastic tool, but what does it take to add your own data to it? Perhaps you want to add some custom meta fields to a shop order or product? Fortunately, it’s pretty simple! Add the code below to your theme’s functions.php file to try it out.

Add Custom Meta to the Shop Order API Response

This will add an iconic_custom_meta key to your API response for a shop order.

Add Custom Meta to the Product API Response

This will add an iconic_custom_meta key to your API response for a product.

API Filter Changes in WooCommerce 2.7

It looks like some of these filters will be changing in WooCommerce 2.7. They are still similar, but the above examples would look more like this:

Note the _object suffix on the filter names, and the second parameter in the functions is now named $object.

Summary

WooCommerce has these prepare filters for most of their API responses. Note that the format of them is woocommerce_rest_prepare_{$type}, where $type is a post type or taxonomy name (like product_cat). In WooCommerce 2.7 some of these filters also have a _object suffix.

Leave a Reply

1 Comment on "Extend the WooCommerce API"

avatar
  Subscribe  
newest oldest most voted
Notify of
Match Stick
Guest

This was working for me before the upgrade to 2.7, now when I try and return custom meta I receive invalid JSON data.