Extend the WooCommerce API

12 Shares

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.

12 Shares

1 Comment

  1. Match Stick says:

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

Leave a Reply

Your email address will not be published. Required fields are marked *