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/

/**
* Save variations.
*
* @param WC_Product $product
* @param WP_REST_Request $request
* @return bool
* @throws WC_REST_Exception
*/
protected
function save_variations_data($product, $request) {

   // EANCODE
   if (isset($variation['ean_code'])) {
       update_post_meta($variation_id, '_ean_code', wp_kses_post($variation['ean_code']));
   }

}

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)

'variations' => [
        [
            'regular_price' => '19.99',
            'ean_code' => '133325325',
            'stock_amsterdam' => 2,
            'stock_den_bosch' => 7,
            'stock_alkmaar' => 9,
            'attributes' => [
                [
                    'id' => 13,
                    'option' => 'S'
                ],
            ]
        ],

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!

About the Author

Damar Anggoro

Voyager lv. 0

Born and raised in Indonesia. I have spent 10 years in Europe and wandered around. Currently living and exploring Bali but have to spend 1 month per year in Europe. Father of one amazing baby girl.

View All Articles