Search code examples
phpwordpresswoocommerceordersaccount

Get Same download links in WooCommerce My account Downloads than Single Order view


In the order section, the download link for downloadable products is as follows:

http://localhost/?download_file=2922&order=wc_order_3av4Ev4PlUyLw&uid=e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855&key=2a89e5ba-4203-4596-8e5b-04b7f66cbb79

But in the downloads section, the download link is as follows - which requires an email, otherwise the file will not be downloaded and an error will be displayed:

http://localhost/wp/?download_file=2922&order=wc_order_3av4Ev4PlUyLw&email&key=2a89e5ba-4203-4596-8e5b-04b7f66cbb79

Order section code:

$order = wc_get_order( 12 );
$downloads = $order->get_downloadable_items();

Downloads section code:

$downloads = wc_get_customer_available_downloads($user_id);

Because most of my users have not registered their email, that's why they are having problems. I want it to be displayed in the download link like the order link.


Solution

  • You can use woocommerce_customer_get_downloadable_products filter hook to make changes to Customer My Account Downloads.

    As you want to get the same download link structure than the download links accessible from customer single order view, we can query all customer orders, use get_downloadable_items() method to get the downloads from each order and merge them, to replace wc_get_customer_available_downloads() function.

    Here is the code:

    add_filter( 'woocommerce_customer_get_downloadable_products', 'alter_customer_downloads_links', 10, 1 );
    function alter_customer_downloads_links( $downloads ) {
        // Only on front-end my account downloads, if there are downloads
        if ( ! is_wc_endpoint_url('downloads') || !$downloads ) { 
            return $downloads;
        }
    
        // Get customer orders
        $customer_orders = wc_get_orders( array(
            'limit'         => -1,
            'customer'      => get_current_user_id(),
            'status'        => wc_get_is_paid_statuses(),
        ) );
    
        $downloads = array(); // Reset downloads
    
        foreach($customer_orders as $order ) {
            // Merge downloadable items
            $downloads = array_merge( $downloads, $order->get_downloadable_items() ); 
        }
        return $downloads;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.