1

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:

enter image description here

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:

enter image description here

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'];)

A3O
  • 513
  • 12
  • 29

1 Answers1

2

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • @A3O OK Updated and tested… **It works nicely with the EXIF title** (There was a little mistake in my code)… **First be sure that there is not downloads in your product removing them (and updating)**. – LoicTheAztec Oct 24 '18 at 14:10
  • Thanks but I think we maybe mis understand each other. What I'm trying to achieve is to set the Post Title / Product Title from the EXIF image title meta tag. Anyhow, it is not working at my end. When no Product Title (Post Title) is given it is simply not save at all. What am I doing wrong here? – A3O Oct 24 '18 at 15:43
  • @A3O No mis understanding… That is what I have set in the function. I am getting and setting the EXIF image title meta tag, which is `$img_meta = wp_get_attachment_metadata( $thumb_id, false );` and `$file_title = $img_meta['image_meta']['title'];` … And it works in my last code version. – LoicTheAztec Oct 24 '18 at 16:58
  • I'm feeling so stupid but..... where is the Post/Product title set, you know the title you have to enter yourself normally when creating a new product. The one that resides at the top of the Add new Product page. I have make an edit to my question. Maybe it clarifies things. Sorry, but I don't understand it. – A3O Oct 24 '18 at 17:15
  • @A3O The code here is just handling the Downloads, but not the image set for this product or its attachement title (or even the product title). So you can get the download title from the Image Exif meta tag title, from the Image attachement title or from whatever else, but to get it, it need to exist to be available. For me all code updates that I have done work on my test server, so I can help more than that. – LoicTheAztec Oct 24 '18 at 17:52
  • I was under the impression that I made myself clear in an early comment about what was left to create an auto Product creation process. I do know that in itself it is a different question. Maybe not that smart to mix it with the original issue, which works by the way very good. On product save the download is created and is stored in the database the in the same way as wen creating the product "by hand". If you wish I will remove my edit and comments regarding the Product name part and try to figure out a way to do that. Please let me know if you want that. Thank for helping me. – A3O Oct 24 '18 at 19:32
  • @LoicTheAztec I've got a seperate question but somewhat relating to this. How do you update existing WC_Product_Download objects? I want to update the file url – S.A Nov 02 '19 at 00:16