1. Home
  2. WP User Frontend Pro
  3. Tutorials
  4. WooCommerce Downloadable Product

WooCommerce Downloadable Product

**Important Note**

If you are using the latest version of WPUF, then you don’t have to go through all the coding outlined below in this document.

The latest version of WPUF has built-in features for uploading downloadable WooCommerce products that you can easily choose to activate for your website. Your users can now easily select to upload a downloadable product and its associated files from the frontend!

Check this link on how you can Add WooCommerce downloadable products using WPUF!

 

How to manually create WooCommerce Downloadable Product

Here we are adding a file upload field in our form and we are giving the meta key as woo_files. You can change it as you want, but the code reference should be changed according to that also.

WooCommerce Downloadable product with WP User Frontend PRO

You’ll have to add the following lines to your theme’s functions.php

/**
* Update the downloadable file array with appropriate meta values
*
* @param int $post_id
* @return void
*/
function wpufe_woo_file_paths( $post_id ) {
if ( isset( $_POST['wpuf_files']['woo_files'] ) ) {
$files = $_POST['wpuf_files']['woo_files'];
$woo_files = array();

foreach ($files as $file_id) {
$file_url = wp_get_attachment_url( $file_id );
$woo_files[md5( $file_url )] = array(
'file' => $file_url,
'name' => basename( $file_url )
);
}

update_post_meta( $post_id, '_downloadable_files', $woo_files );
}
}

add_action( 'wpuf_add_post_after_insert', 'wpufe_woo_file_paths' );
add_action( 'wpuf_edit_post_after_update', 'wpufe_woo_file_paths' );

When we are creating or updating the the product, we are saving the file URL’s in WooCommerces files meta key _file_paths.

We need to insert a hidden field with meta key _downloadable_files and value should be yes, that will make the product as downloadable. Simple enough 🙂

Was this article helpful to you? Yes 9 No 4

How can we help?