Automating the creation of simple virtual downloadable products in WooCommerce can significantly streamline your workflow, especially if your store relies heavily on selling digital products. This guide will show you how to automatically create a simple virtual downloadable product whenever a new image is uploaded to your WordPress media library. This approach can save time and ensure consistency in your product listings.
Benefits of Automation
Automating the creation of virtual downloadable products can provide several benefits:
- Efficiency: Save time by automating repetitive tasks.
- Consistency: Ensure all your digital products are created with the same settings and attributes.
- Scalability: Easily manage large volumes of digital products without manual intervention.
Step 1: Add the Code to Your Theme’s Functions File
To achieve this automation, you will need to add a custom function to your theme’s functions.php file. If you don’t have a child theme, you can create one using our free child theme generator.
Step 2: Create the Function
Add the following code to your theme’s functions.php file:
/*
* * Snippet: How to Create a Simple Virtual Downloadable Product Automatically When a New Image Is Uploaded – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1371
* Tested with WooCommerce 10.7.0
* "Automatically create virtual downloadable product when image is uploaded"
*/
function wcsuccess_create_downloadable_product($attachment_id) {
// Get attachment details
$attachment = get_post($attachment_id);
$file_url = wp_get_attachment_url($attachment_id);
$file_title = $attachment->post_title;
// Create the product
$post_id = wp_insert_post(array(
'post_title' => $file_title,
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'product',
));
if ($post_id) {
// Set product type to 'simple'
wp_set_object_terms($post_id, 'simple', 'product_type');
// Set downloadable and virtual properties
update_post_meta($post_id, '_downloadable', 'yes');
update_post_meta($post_id, '_virtual', 'yes');
// Add downloadable file
$downloads = array(
array(
'name' => $file_title,
'file' => $file_url,
),
);
update_post_meta($post_id, '_downloadable_files', $downloads);
// Set product price (optional)
update_post_meta($post_id, '_regular_price', '0'); // Set to '0' for free products
// Set stock status
update_post_meta($post_id, '_stock_status', 'instock');
// Set product visibility
update_post_meta($post_id, '_visibility', 'visible');
}
}
// Hook the function into the 'add_attachment' action
add_action('add_attachment', 'wcsuccess_create_downloadable_product');
Explanation
- Function Definition: The
wcsuccess_create_downloadable_productfunction is defined to create a new product whenever a new image is uploaded. - Attachment Details: The function retrieves the uploaded image’s details, including its URL and title.
- Product Creation: The function creates a new WooCommerce product with the retrieved details.
- Product Type: The product type is set to
simple, and it is marked asdownloadableandvirtual. - Downloadable Files: The uploaded image is added as a downloadable file for the product.
- Product Price: The product price is set to
0, making it a free downloadable product (optional). - Stock Status and Visibility: The product is marked as
instockand set tovisible.
Use Cases
- Photography Websites: Automatically create downloadable products for each new image uploaded, allowing customers to purchase digital copies.
- Digital Art Stores: Streamline the process of selling digital artwork by automating product creation.
- Educational Resources: Automatically create downloadable products for educational materials, such as worksheets or e-books, whenever new files are uploaded.
Conclusion
By automating the creation of simple virtual downloadable products, you can save time and ensure consistency across your WooCommerce store. This approach is particularly useful for stores that deal with a high volume of digital products. Simply add the provided code to your theme’s functions.php file, and every time you upload a new image, a corresponding downloadable product will be automatically created.
Final Thoughts
Remember to test this function thoroughly on a staging site before deploying it to your live store to ensure it works as expected. Happy selling!
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Stay In Touch

