Create Product Programmatically

How to create WooCommerce products programmatically

If you want to want to create some products on your WooCommerce shop without your back-office interface, you can do it easily with the code below:

$my_product = new WC_Product_Simple();
$my_product->set_name( 'My new product' );
$my_product->set_status( 'publish' ); 
$my_product->set_catalog_visibility( 'visible' );
$my_product->set_price( 9.90 );
$my_product->set_regular_price( 9.90 );
$my_product->set_sold_individually( true );
$my_product->set_image_id( 1 );
$product->set_category_ids( array( 1 ) );

You can fill these data :

  • set_name() is the title of your product
  • set_status() is the status : publish or draft for example
  • set_catalog_visibility()
  • set_price() is the price displayed for your customers
  • set_image_id() refers to your image ID that will be your featured image
  • set_category_ids() is your categories ids

new WC_Product_Simple() and save() must always be there. The complete documentation can be find here : WC_Product_Simple.

If you need to create variable products programmatically, you can also check this documentation: WC_Product_Variable

Leave a Comment

Your email address will not be published.

You may also like