How to Create a Simple Virtual Downloadable Product Automatically When a New Image Is Uploaded – 2026

Last updated on August 17th, 2024 at 07:07 am

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:

  1. Efficiency: Save time by automating repetitive tasks.
  2. Consistency: Ensure all your digital products are created with the same settings and attributes.
  3. 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

  1. Function Definition: The wcsuccess_create_downloadable_product function is defined to create a new product whenever a new image is uploaded.
  2. Attachment Details: The function retrieves the uploaded image’s details, including its URL and title.
  3. Product Creation: The function creates a new WooCommerce product with the retrieved details.
  4. Product Type: The product type is set to simple, and it is marked as downloadable and virtual.
  5. Downloadable Files: The uploaded image is added as a downloadable file for the product.
  6. Product Price: The product price is set to 0, making it a free downloadable product (optional).
  7. Stock Status and Visibility: The product is marked as instock and set to visible.

Use Cases

  1. Photography Websites: Automatically create downloadable products for each new image uploaded, allowing customers to purchase digital copies.
  2. Digital Art Stores: Streamline the process of selling digital artwork by automating product creation.
  3. Educational Resources: Automatically create downloadable products for educational materials, such as worksheets or e-books, whenever new files are uploaded.
See also  Better Woocommerce Out of Stock Message

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!

0 0 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top
0
Would love your thoughts, please comment.x
()
x
×