WooCommerce: Create Product Programmatically

No matter if this snippet suits your needs or not, it’s still interesting to see how you can create a brand new WooCommerce product programmatically / automatically when a certain even triggers.

In this case studio, we’ll see how to generate a brand new product, set its featured image, price, category, title and a download file as soon as an image is uploaded in the WordPress backend Media section. This would be super useful for photographers for example – simply upload a new image to the WordPress Media Library, and a new product is automatically created.

Of course, you can customize the code and use it with different triggers. For example, you may need to create a new product automatically when each phone product needs always a matching case product, just with a different title. Or maybe you want to tie product creation once an order is placed.

Either way, enjoy!

This product was created automatically, after uploading Calm.jpg to the WordPress Media Library. You find more screenshots below.

PHP Snippet 1: Create Simple Virtual Downloadable Product Automatically When A New Image Is Uploaded @ WordPress Media Library

/**
 * @snippet       AutoCreate Simple Product @ WP Admin
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'add_attachment', 'bbloomer_create_simple_product_automatically', 9999 );

function bbloomer_create_simple_product_automatically( $image_id ) { 
    $product = new WC_Product_Simple();
    $product->set_name( 'Photo: ' . get_the_title( $image_id ) );
    $product->set_status( 'publish' ); 
    $product->set_catalog_visibility( 'visible' );
    $product->set_category_ids( array( 20 ) );
    $product->set_price( 19.99 );
    $product->set_regular_price( 19.99 );
    $product->set_sold_individually( true );
    $product->set_image_id( $image_id );
    $product->set_downloadable( true );
    $product->set_virtual( true );		
    $src_img = wp_get_attachment_image_src( $image_id, 'full' );
    $file_url = reset( $src_img );
    $file_md5 = md5( $file_url );
    $download = new WC_Product_Download();
    $download->set_name( get_the_title( $image_id ) );
    $download->set_id( $file_md5 );
    $download->set_file( $file_url );
    $downloads[$file_md5] = $download;
    $product->set_downloads( $downloads );
	 $product->save();
}

PHP Snippet 2: Create Variable Product Automatically When A New Image Is Uploaded @ WordPress Media Library

/**
 * @snippet       AutoCreate Variable Product @ WP Admin
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_action( 'add_attachment', 'bbloomer_create_variable_product_automatically', 9999 );

function bbloomer_create_variable_product_automatically( $image_id ) {
 
    $product = new WC_Product_Variable();
    $product->set_name( 'Photo: ' . get_the_title( $image_id ) );
    $product->set_status( 'publish' ); 
    $product->set_catalog_visibility( 'visible' );
    $product->set_image_id( $image_id );
    $product->set_category_ids( array( 20 ) );

    $attribute = new WC_Product_Attribute();
	 $attribute->set_id( wc_attribute_taxonomy_id_by_name( 'pa_color' ) );
	 $attribute->set_name( 'pa_color' );
	 $attribute->set_options( array( 29, 30 ) ); // color att terms
	 $attribute->set_position( 1 );
	 $attribute->set_visible( 1 );
	 $attribute->set_variation( 1 );
	 $attributes[] = $attribute;
	 $product->set_attributes( $attributes );

    $product->save();

    $variation1 = new WC_Product_Variation();
	 $variation1->set_parent_id( $product->get_id() );
	 $variation1->set_attributes( array( 'pa_color' => 'blue' ) );
	 $variation1->set_regular_price( 99.99 );
	 $variation1->save();

    $variation2 = new WC_Product_Variation();
	 $variation2->set_parent_id( $product->get_id() );
	 $variation2->set_attributes( array( 'pa_color' => 'red' ) );
	 $variation2->set_regular_price( 197.99 );
	 $variation2->save();

}

Screenshots

1. Image upload from Media Library (Calm.jpg)

2. Automatic product creation

Where to add custom code?

You should place PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my guide "Should I Add Custom Code Via WP Editor, FTP or Code Snippets?" and my video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything went as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting.

If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

6 thoughts on “WooCommerce: Create Product Programmatically

  1. Hi Rodolfo Melogli,

    Thank you for this useful snippet. I am using 7.4 and its 9/7/22 and this was just perfect for my requirement!

    Heather Lynn

  2. I couldn’t get this to work. I admit I loathe WP and find it completely dyslexic – so I may be biased. But no – unfortunately spent the whole day on this and nothing worked. 9/7/22

  3. Hi, thanks for your post.

    Is there any way to add brand name to a product?

    thanks again

    1. Hi Javier thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

Your email address will not be published. Required fields are marked *