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