I’m writing a plugin that will automatically import products via JSON API. I need to programatically create/update products.

I did a research and it seems everyone is using wp_insert_post() and update_post_meta() for this, ie this and this

However, I found that there’s the class-wc-api-products, where are various functions for adding/editing products and variations.

Is there any reason why people don’t use something like:

$wc = new WC_API_Products();
$wc->create_product( $data );

It seems to me more future-proof to use native WC classes for this, am I wrong?

1 Answer
1

Technically speaking, Woocommerce products are just another WordPress post object with a customized post type (which is product in this case). If you take a look at the create_product function inside class-wc-api-products.php, you’ll see this line:

// Attempts to create the new product
$id = wp_insert_post( $new_product, true );

So the API ends up using wp_insert_post eventually. I guess you can do both but it’s obviously using what WooCommerce API provides to create a WooCommerce product is a better way to go.

Leave a Reply

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