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

WooCommerce Downloadable Product

You can’t create WooCommerce downloadable right out-of-the-box with WP User Frontend PRO because WooCommerce saves the downloadable file paths in an array. But WP User Frontend saves files/attachments ID’s in custom field. So we need a special tweaking to save them in the array.

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?