Add new product in WooCommerce programmatically

/**
 * wc_add_new_product.
 */
function wc_add_new_product( $title, $description = '', $short_description = '' ) {

	$new_product_post = array(
		'post_title'    => $title,
		'post_content'  => $description,
		'post_excerpt'  => $short_description,
		'post_type'     => 'product',
		'post_status'   => 'publish',
	);

	// Insert the post into the database
	if ( 0 != ( $new_product_id = wp_insert_post( $new_product_post ) ) ) {
		update_post_meta( $new_product_id, '_visibility', 'visible' );
		update_post_meta( $new_product_id, '_stock_status', 'instock' );
	}

	return $new_product_id;
}

Leave a Comment