Extending Woocommerce API Product Variation

Hi guys, this post will be about on how to extend your woocommerce API on product variation level. We have another post on how extend your woocommerce API on product level. But this post will be about extending on product variation level. So when would you need to do this? Here in our web-shop market in Netherlands, most of our clients have their product push from another ERP (enterprise resource planning) through woocommerce API.

One example is one of our clients has OrderSolutions for managing their product. One of the client request was to have EAN Code (for Google Shopping) on product variation level. Normally you would use the available SKU fields, however in this case the field is already used. And the goal was to make a new field so you can have as much filed as you like. Here are the step.

Register you custom fields in wocommerce product variation level

I have written another blog post for this, please kindly read it

Extend your Woocommerce API

It is not recommended to edit the core files. However, I have been looking everywhere and this is the only solution I managed to pull of. Of course I will post a better solution if I have them. Place this code in the class-wc-rest-products-controller.php, inside the save_variation_data function. This file is located in woocommerce/includes/api/

This code above will basically allow you to add new postmeta data on the product variation. In the case above it will be store with a meta_key = “_ean_code”. Essentially, a product variation is stored exactly the same as a normal product in the database. Therefore, it is just a matter of adding the value in wp_postmeta table. After you placed the code above you can now create a new product with a woocommerce API with the code below (PHP)

At the ‘variations’ node, you can add a new node called ‘ean_code’.

If you now asking “How do I make this field available on my other API endpoints (i.e get products)?”. Lucky for you, I have that cover on my other post. Please check it.
See you on my next post!

Leave a Reply