nabs_kk
nabs_kk

Reputation: 35

Playing audio/video files instead of downloading them on Woocommerce "My Account" downloads section

I have a quick question in regards to WooCommerce and downloadable files. I would like to make the purchased products play in My Account section rather than easily download them (the exact opposite of how WooCommerce handles downloadable/virtual products).

I have managed to locate the code that handles the view of the "Downloadable products" in My Account section (templates/order/order-downloads.php), however I can't make it work, since the link results in the "safe version"

https://example.com/?download_file=XXXX&order=wc_order_cSzbzLqustvlw&email=XXXXXXXX&key=XXXXX

Instead of the friendly/unsecured file URL

https://example.com//my-music.mp3


I have changed this line to make it work as audio shortcode, however it doesn't play since it doesn't get the full file URL.

case 'download-file':
    echo do_shortcode('[audio src="' . esc_url( $download['download_url'] ) . '"]');
    break;

I know it's not the best approach to change the code there, however I just want to make it work in a way that the customer can play the file, but can't download it directly. Can you help me how to achieve this?

EDIT

With the help of Martin Mirchev I was able to get the following:

case 'download-file':
    $url_file = esc_url( $download['file']['file'] );
    $address = explode(".", $url_file);
    if ($address[2] == "mp3") {
    echo "<div id='content' class='fancybox-hide' style='min-width:450px; min-height:250px;'>";
    echo do_shortcode('[audio src="' . esc_url( $download['file']['file'] ) . '"]');
    echo "</div>";
    echo "<a href='#content' class='ari-fancybox'>Listen to MP3</a>";
    }

Would there be a possibility to apply the same customization in WooCommerce as opposed to overwriting the template file?

Upvotes: 2

Views: 759

Answers (1)

7uc1f3r
7uc1f3r

Reputation: 29614

No need to overwrite template files, you can use the woocommerce_account_downloads_column_{$column_id} composite action hook, where $column_id is download-file

So you get:

function action_woocommerce_account_downloads_column_download_file( $download ) {
    // Targeting view order pages only
    if ( ! is_wc_endpoint_url( 'downloads' ) ) return;

    // Getters
    $url_file = esc_url( $download['file']['file'] );
    $address = explode( ".", $url_file );

    // Compare
    if ( $address[2] == "mp3" ) {
        echo "<div id='content' class='fancybox-hide' style='min-width:450px; min-height:250px;'>";
        echo do_shortcode( '[audio src="' . $url_file . '"]' );
        echo "</div>";
        echo "<a href='#content' class='ari-fancybox'>Listen to MP3</a>";
    } else {
        echo '<a href="' . esc_url( $download['download_url'] ) . '" class="woocommerce-MyAccount-downloads-file button alt">' . esc_html( $download['download_name'] ) . '</a>';
    }
}
add_action( 'woocommerce_account_downloads_column_download-file', 'action_woocommerce_account_downloads_column_download_file', 10, 1 );

Upvotes: 1

Related Questions