Today, we are going to learn about adding simple products in WooCommerce programmatically. This is a beginner guide, so feel free to use the code on your own websites and modify it as you like. If you plan on executing this code on a live website, we strongly suggest speaking with a professional developer. To add a simple product in WooCommerce programmatically, copy and post the following code:
$productId = wp_insert_post( array(
'post_title' => 'Simple Product!',
'post_content' => 'Simple product',
'post_status' => 'publish',
'post_type' => "product",
) );
wp_set_object_terms( $productId, 'simple', 'product_type' );
'post_title' => 'Simple Product!',
'post_content' => 'Simple product',
'post_status' => 'publish',
'post_type' => "product",
) );
wp_set_object_terms( $productId, 'simple', 'product_type' );
If everything goes right, a new product should appear on the product listing page in the WooCommerce dashboard.
That’s the end of this tutorial; stay tuned for more WordPress and WooCommerce tutorials like this one.