• Resolved yliya14

    (@yliya14)


    Hello. I’ve been trying to create a downloadable products post form for a week now. I would like users to publish downloadable products, but the problem is that I don’t know the correct meta_key of the downloadable products so that they are then available in WooCommerce for download via a link. I have already seen the question with the price, and your screenshot https://monosnap.com/file/jESwutPy6qygMOy3jSJ54tuIBxgbno. But I still can’t. Sorry to bother you. And if something is not clear, I will write a post again, since I used automatic translation from Russian.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Support Pawel – WPMU DEV Support

    (@wpmudev-support9)

    Hello @yliya14 !

    Hope you’re doing well today!

    I’ve checked how to do that in the case of a downloadable product and I see that there are some fields which can be added easily using the method I’ve explained in the other ticket (like setting the _downloadable field to yes etc.), but there’s one field _downloadable_files which contains more data than just the link to the file (it’s a serialised array) so it can’t be easily added from the form settings.

    However, it should be possible to do that using a snippet which will convert the data from the Upload field to the required data in WooCommerce. I’ve already requested our Second Line Support team to take a look and see if they can prepare such snippet for you. Once we hear back from them we’ll share the details here.

    Kind regards,
    Pawel

    Thread Starter yliya14

    (@yliya14)

    Thanks. I will be waiting for an answer! Even if it doesn’t work, please write here that it’s not possible.

    Plugin Support Dimitris – WPMU DEV Support

    (@wpmudev-support6)

    Hello there @yliya14

    Our devs provided a simple customization via the following code that can be used as a MU-plugin (https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-plugins):

    <?php
    add_action( 'forminator_post_data_field_post_saved',  'wpmudev_auto_add_downloadable_file', 10, 4);
    function wpmudev_auto_add_downloadable_file( $post_id, $field, $data, $cls ){
    	$product = wc_get_product( $post_id );
    	// Get downloads (if there is any)
        $downloads = (array) $product->get_downloads(); 
        // Only added once (avoiding repetitions
        if( sizeof($downloads) == 0 ){
            // Get post upload data
            $thumb_url = get_post_meta( $post_id, 'upload-1', true );
    		if($thumb_url){
    			// Prepare download data
    			$file_title = basename($thumb_url);
    			$file_md5   = md5($thumb_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($thumb_url);
    
    			$downloads[$file_md5] = $download; // Insert the new download to the array of downloads
    			$product->set_downloads($downloads); // Set new array of downloads
    			$product->set_downloadable('yes'); // To set the product type as downloadable
    			$product->save();
    		}
        }
    }

    To make the code work you will need to create an upload field and map it in the postdata custom fields like the following images:
    https://monosnap.com/file/NTwiiLDu6lKarK0Bwk9lAvNKRkD0Fj
    https://monosnap.com/file/4RYLY9Vv7M1USu9c4sfNuEzAFAr4AQ

    With the code we are setting the product as downloadable here $product->set_downloadable('yes'); and the rest of the code is to set the download for the product.

    Thank you,
    Dimitris

    Thread Starter yliya14

    (@yliya14)

    Thank you very much! Everything worked out! You are the best!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.