Update 2 (Added an if statement to allow download generation to only one file)
The following code will auto add a downloadable file made from the product image (The download title comes from the EXIF data title).
You should better use dedicated woocommerce_admin_process_product_object
action hook and available CRUD objects and getters / setters methods introduced with woocommerce 3 this way:
add_action( 'woocommerce_admin_process_product_object', 'auto_add_downloadable_file', 50, 1 );
function auto_add_downloadable_file( $product ){
// Get downloads (if there is any)
$downloads = (array) $product->get_downloads();
// Only added once (avoiding repetitions
if( sizeof($downloads) == 0 ){
// Get post thumbnail data
$thumb_id = get_post_thumbnail_id( $product->get_id() );
$src_img = wp_get_attachment_image_src( $thumb_id, 'full');
$img_meta = wp_get_attachment_metadata( $thumb_id, false );
// Prepare download data
$file_title = $img_meta['image_meta']['title'];
$file_url = reset($src_img);
$file_md5 = md5($file_url);
$download = new WC_Product_Download(); // Get an instance of the WC_Product_Download Object
// Set the download data
$download->set_name($file_title);
$download->set_id($file_md5);
$download->set_file($file_url);
$downloads[$md5_num] = $download; // Insert the new download to the array of downloads
$product->set_downloads($downloads); // Set new array of downloads
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
You could also check if product is downloadable using is_downloadable() method in an IF
statement on start inside the function.