This will give you a base to start:
function generate_simple_product() {
$name="My Product Name";
$will_manage_stock = true;
$is_virtual = false;
$price = 1000.00;
$is_on_sale = true;
$sale_price = 999.00;
$product = new \WC_Product();
$image_id = 0; // Attachment ID
$gallery = self::maybe_get_gallery_image_ids();
$product->set_props( array(
'name' => $name,
'featured' => false,
'catalog_visibility' => 'visible',
'description' => 'My awesome product description',
'short_description' => 'My short description',
'sku' => sanitize_title( $name ) . '-' . rand(0, 100), // Just an example
'regular_price' => $price,
'sale_price' => $sale_price,
'date_on_sale_from' => '',
'date_on_sale_to' => '',
'total_sales' => 0,
'tax_status' => 'taxable',
'tax_class' => '',
'manage_stock' => $will_manage_stock,
'stock_quantity' => $will_manage_stock ? 100 : null, // Stock quantity or null
'stock_status' => 'instock',
'backorders' => 'no',
'sold_individually' => true,
'weight' => $is_virtual ? '' : 15,
'length' => $is_virtual ? '' : 15,
'width' => $is_virtual ? '' : 15,
'height' => $is_virtual ? '' : 15,
'upsell_ids' => '',
'cross_sell_ids' => '',
'parent_id' => 0,
'reviews_allowed' => true,
'purchase_note' => '',
'menu_order' => 10,
'virtual' => $is_virtual,
'downloadable' => false,
'category_ids' => '',
'tag_ids' => '',
'shipping_class_id' => 0,
'image_id' => $image_id,
'gallery_image_ids' => $gallery,
) );
$product->save();
return $product;
}
The code above is taken from WooCommerce Smooth Generator, made by WooCommerce itself. It’s mostly used for testing.
https://github.com/woocommerce/wc-smooth-generator
Example:
// Generate WC_Product object and save it to database
// 70% change generated product is simple
// 30% chance generated product is variable
$product = \WC\SmoothGenerator\Generator\Product::generate();
// Returns WC_Product object of Simple product and don't save it to database
$product = \WC\SmoothGenerator\Generator\Product::generate_simple_product();
// Returns WC_Product object of Variable Product and saves it to database
$variable_product = \WC\SmoothGenerator\Generator\Product::generate_variable_product();
Src: https://github.com/woocommerce/wc-smooth-generator/blob/master/includes/Generator/Product.php
If you want to create products programatically, you can edit the Product generator class above with your needs.