In my solution I want to automate the product creation as much as possible. One time saver is, in my opinion, to auto add the downloadable file to the product.
I have created this function:
function fcsp_add_downloadable_file($post_id, $post, $update){
$post_thumbnail_id = get_post_thumbnail_id( $post_id );
$url = get_site_url()."/wp-content/uploads/".get_the_date('Y')."/".get_the_date('m')."/".$filename_only = basename( get_attached_file( $post_thumbnail_id ) );
update_post_meta($post_id, '_downloadable_files' , $url);
}
add_action( 'save_post', 'fcsp_add_downloadable_file', 99, 3 );
I can see when I update the product that the file path is saved to the _downloadable_files meta key. However it is just plain text and not in the way woocommerce stores it. See screenshot (this is from another product created with the Woo Add Product interface:
It is also not recognized by woocommerca as a downloadable file. Any help on fixing this is much appreciated.
EDIT: Part two
This is the product title to be set:
We have to get it from the EXIF meta tag "title" from the image and has te be set as the product name before or while saving the product. ($filemeta['image_meta']['title'];
)
Update 2 (Added an if statement to allow download generation to only one file)
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:Code goes in function.php file of your active child theme (or active theme). Tested and works.