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 ) );
$my_product->save();

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
A LIRE AUSSI  What is the cause about "Internal Server Error" ?

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
gdpr
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.