For online stores that sell digital images, offering various sizes at different price points can enhance the customer experience and maximize revenue. This guide will show you how to automatically create variable virtual downloadable products with different pricing based on image size whenever a new image is uploaded to your WordPress media library.
Benefits of Automating Variable Product Creation
- Efficiency: Streamline the product creation process.
- Consistency: Ensure all products follow a standard format.
- Revenue: Cater to different customer needs and budgets with tiered pricing.
Step 1: Add Code to Functions.php
To automatically create variable virtual downloadable products, you will need to add the following code 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.
/*
* Snippet: How to Create Variable Virtual Downloadable Products Automatically When a New Image Is Uploaded – 2026
* Author: John Cook
* URL: https://wcsuccessacademy.com/?p=1373
* Tested with WooCommerce 10.7.0
* "Automatically Create Variable Virtual Downloadable Products"
*/
function wcsuccess_create_variable_virtual_product_on_image_upload($metadata, $attachment_id) {
$attachment = get_post($attachment_id);
$upload_dir = wp_upload_dir();
$file_path = $upload_dir['basedir'] . '/' . $attachment->guid;
// Create a new product
$product = new WC_Product_Variable();
$product->set_name($attachment->post_title);
$product->set_status('publish');
$product->set_catalog_visibility('visible');
$product->set_description('Automatically created product for ' . $attachment->post_title);
$product->set_virtual(true);
$product->set_downloadable(true);
$product->save();
// Define image sizes and prices
$sizes = [
'Small' => 5.00,
'Medium' => 10.00,
'Large' => 15.00
];
// Add attributes and variations
$attributes = new WC_Product_Attribute();
$attributes->set_name('Image Size');
$attributes->set_options(array_keys($sizes));
$attributes->set_visible(true);
$attributes->set_variation(true);
$product->set_attributes([$attributes]);
$product->save();
foreach ($sizes as $size => $price) {
$variation = new WC_Product_Variation();
$variation->set_parent_id($product->get_id());
$variation->set_attributes(['Image Size' => $size]);
$variation->set_regular_price($price);
$variation->set_virtual(true);
$variation->set_downloadable(true);
$variation->set_downloads([[
'name' => $size . ' - ' . $attachment->post_title,
'file' => $file_path
]]);
$variation->save();
}
return $metadata;
}
add_filter('wp_generate_attachment_metadata', 'wcsuccess_create_variable_virtual_product_on_image_upload', 10, 2);
Step 2: Ensure Correct File Path and Permissions
Make sure the file path and permissions are correctly set to allow WooCommerce to access the uploaded images.
Step 3: Test the Functionality
Upload a new image via the WordPress media library to test if the code works correctly. The image should automatically create a variable virtual downloadable product with different sizes and corresponding prices.
Custom thumbnail sizes
You can integrate custom thumbnail sizes into the pricing of your variable virtual downloadable products to further enhance your offerings. By creating custom thumbnail sizes, you can assign different prices based on the resolution and quality of the images. This allows customers to choose the exact size that fits their needs and budget, creating a more personalized shopping experience. To learn how to create custom thumbnail sizes, refer to our detailed guide here. By combining these custom sizes with the automated product creation process, you can efficiently manage a diverse range of digital products in your WooCommerce store, maximizing both customer satisfaction and revenue.
Conclusion
By following the steps above, you can automate the creation of variable virtual downloadable products in WooCommerce whenever a new image is uploaded. This approach saves time, ensures consistency, and enhances the customer experience by offering different sizes and price points for digital images.
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

