Title: Programatically adding product variations?
Last modified: September 1, 2016

---

# Programatically adding product variations?

 *  [davedog33](https://wordpress.org/support/users/davedog33/)
 * (@davedog33)
 * [10 years ago](https://wordpress.org/support/topic/programatically-adding-product-variations/)
 * Hello,
 * Is there a way to programatically insert a variation on a product? Since they’re
   just the custom post type “product_variation” then I could just use wp_insert_post
   right? The problem is, attaching that post as metadata to a product.
 * What would I put in add_post_meta on product to add. Something like:
    update_post_meta(
   product_id, variation_meta_key, array(wp_insert_post))
 * IDK what woocommerce uses as the meta key for the variations, nor how in what
   format the variations are stored there. Is there a way faster way to do this??
 * Thank you!
 * [https://wordpress.org/plugins/woocommerce/](https://wordpress.org/plugins/woocommerce/)

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Thread Starter [davedog33](https://wordpress.org/support/users/davedog33/)
 * (@davedog33)
 * [10 years ago](https://wordpress.org/support/topic/programatically-adding-product-variations/#post-7496534)
 * By the way the reason I need to do this is because I’m interacting with a third
   party API to synchronise their variations to woocommerce’s variation’s.
 *  Plugin Contributor [Mike Jolley](https://wordpress.org/support/users/mikejolley/)
 * (@mikejolley)
 * [10 years ago](https://wordpress.org/support/topic/programatically-adding-product-variations/#post-7496544)
 * You could use the REST API? Or yes, you can use meta data. If you create a variation
   manually and export you can see the data you’ll need to add.
 *  Thread Starter [davedog33](https://wordpress.org/support/users/davedog33/)
 * (@davedog33)
 * [10 years ago](https://wordpress.org/support/topic/programatically-adding-product-variations/#post-7496547)
 * Thanks 🙂 I did a little hunting and found the ajax hook that is responsible 
   for letting the user add a variation. Led me to this code: [https://gist.github.com/qutek/064bf7b44dfd2c6f46b2](https://gist.github.com/qutek/064bf7b44dfd2c6f46b2)
 * and it looks like all you have to do is specify the post_parent as the current
   product’s ID. 🙂
 * I’ll try it out, but don’t see why it shouldn’t work.
 *  [Frank Laarakker](https://wordpress.org/support/users/franklaarakker/)
 * (@franklaarakker)
 * [9 years, 2 months ago](https://wordpress.org/support/topic/programatically-adding-product-variations/#post-9056798)
 * I think you’re best off sticking to the REST API.On other places you will find
   PHP code that actually directly creates posts and meta data. I have found this
   approach to be highly unreliable and may actually corrupt your product database.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Programatically adding product variations?’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 4 replies
 * 3 participants
 * Last reply from: [Frank Laarakker](https://wordpress.org/support/users/franklaarakker/)
 * Last activity: [9 years, 2 months ago](https://wordpress.org/support/topic/programatically-adding-product-variations/#post-9056798)
 * Status: not resolved