You can create a WooCommerce product programmatically using the WC_Product_Simple class (or the variable/other product classes), setting its name, price, category, and image through setter methods, then saving it — triggered by any event you choose, like a new image upload.
Why Automate Product Creation
- Bulk or event-driven product creation saves significant manual data-entry time
- It enables workflows like auto-generating a product listing whenever a new photo, design, or asset is uploaded
- It’s the foundation for syncing products from an external system or API into WooCommerce
Step 1: Create a Simple Product Programmatically
/** Automatically create a simple downloadable product whenever a new image is uploaded */
add_action( 'add_attachment', 'generate_product_from_uploaded_image', 9999 );
function generate_product_from_uploaded_image( $attachment_id ) {
$product = new WC_Product_Simple();
$product->set_name( 'Print: ' . get_the_title( $attachment_id ) );
$product->set_status( 'publish' );
$product->set_catalog_visibility( 'visible' );
$product->set_category_ids( array( 34 ) );
$product->set_price( 24.99 );
$product->set_regular_price( 24.99 );
$product->set_sold_individually( true );
$product->set_image_id( $attachment_id );
$product->set_virtual( true );
$product->set_downloadable( true );
$image_src = wp_get_attachment_image_src( $attachment_id, 'full' );
$file_url = reset( $image_src );
$download = new WC_Product_Download();
$download->set_name( get_the_title( $attachment_id ) );
$download->set_id( md5( $file_url ) );
$download->set_file( $file_url );
$product->set_downloads( array( md5( $file_url ) => $download ) );
$product->save();
}
Troubleshooting
Products are created but missing the featured image
Confirm set_image_id() receives a valid attachment ID and that the attachment is a genuine image, not a different file type accidentally triggering the hook.
The hook fires multiple times for one upload
Some media workflows (like automatic thumbnail generation) can trigger add_attachment more than once — add a check to confirm you’re only processing the original upload, not derived sizes.
Frequently Asked Questions
Can I create variable products this way too?
Yes, using WC_Product_Variable along with creating individual WC_Product_Variation objects for each variation, though the setup is more involved than a simple product.
What other events can trigger automatic product creation?
Any WordPress or WooCommerce hook works — a new order being placed, a form submission, a custom REST API call, or a scheduled cron job syncing from an external source.
Is this safe to run on a high-traffic site?
Yes, as long as the triggering event isn’t itself extremely high-frequency — creating a product involves standard WooCommerce database writes, the same as manual creation through wp-admin.
Log every automatically created product ID somewhere queryable, since debugging an unexpected duplicate or missing product is much easier when you can trace exactly which upload or event triggered its creation in the first place. A simple custom database table or even post meta on the triggering event works fine for this.
Conclusion
The WC_Product_Simple class makes it straightforward to create a WooCommerce product programmatically, and the same pattern extends to virtually any trigger event you can hook into.
See also How to Define WooCommerce Product Settings Programmatically. Then read How to Create a WooCommerce Order From Another Website.
Prefer no code? All-in-One Utilities Woo Add-on adds over 50 WooCommerce features, so you can switch them on without code.