• davedog33

    (@davedog33)


    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://ww.wp.xz.cn/plugins/woocommerce/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter davedog33

    (@davedog33)

    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

    (@mikejolley)

    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

    (@davedog33)

    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

    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.

    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.