5

I am using WooCommerce Plugin. In the downloadable product, how can I display the filename instead of product name? Now the Downloadable product looks like this in the page "My Account":

title-->file1
title-->file2. 

Now I need to display that listing of file into filename. For example: in the "Order" page it should be like this:

title-->Installer.zip(filename)
title-->Runner.zip(filename)

How can I list each filename in the downloadable product list?

I am using the WooCommerce 2.0.4

<?php
/**
 * My Account
 */

global $woocommerce;
?>

<?php $woocommerce->show_messages(); ?>

<p><?php printf( __('Hello, <strong>%s</strong>. From your account dashboard you can view your recent orders, manage your shipping and billing addresses and <a href="%s">change your password</a>.', 'kinetico'), $current_user->display_name, get_permalink(woocommerce_get_page_id('change_password'))); ?></p>

<?php do_action('woocommerce_before_my_account'); ?>

<?php if ($downloads = $woocommerce->customer->get_downloadable_products()) : ?>
<h2><?php _e('Available downloads', 'kinetico'); ?></h2>
<ul class="digital-downloads">

    <?php foreach ($downloads as $download) : ?>

        <li><?php if (is_numeric($download['downloads_remaining'])) : ?><span class="count"><?php echo $download['downloads_remaining'] . _n(' download Remaining', ' downloads remaining', $download['downloads_remaining'], 'kinetico'); ?></span><?php endif; ?> <a href="<?php echo esc_url( $download['download_url'] ); ?>"><?php echo $download['download_name']; ?></a></li>
     <?php echo    $download['download_name']; ?>
    <?php endforeach; ?>

</ul>
<?php endif; ?> 
<?php add_filter( 'woocommerce_available_download_link', 'filter_wc_downloads_so_16142748', 10, 2 );

function filter_wc_downloads_so_16142748( $link, $download )
{
    // Create a WC_Order object and get the file URLs for this product
    $order = new WC_Order( $download['order_id'] );
    $download_file_urls = $order->get_downloadable_file_urls( 
        $download['product_id'], 
        null, 
        $download['download_id'] 
    );

    // Check each download URL and compare with the current URL 
    // $key contains the real file URL and $value is the encoded URL
    foreach( $download_file_urls as $key => $value )
    {
        if( $value == $download['download_url'] )
        {
            $url_parts = explode( '/', parse_url( $key, PHP_URL_PATH ) );
            $file_name = end( $url_parts );
            $link = '<a href="' 
                . esc_url( $download['download_url'] ) 
                . '">' 
                . $download['download_name'] 
                . '</a> <small>( ' 
                . $file_name 
                . ' )</small>';
        }               
    }
    return $link;
}?>
<h2><?php _e('Recent Orders', 'kinetico'); ?></h2>
<?php woocommerce_get_template('myaccount/my-orders.php', array( 'recent_orders' => $recent_orders )); ?>

<h2><?php _e('My Address', 'kinetico'); ?></h2> 
<p><?php _e('The following addresses will be used on the checkout page by default.', 'kinetico'); ?></p>
<?php woocommerce_get_template('myaccount/my-address.php'); ?>

<?php
do_action('woocommerce_after_my_account');
add_filter( 'woocommerce_available_download_link', 'filter_wc_downloads_so_16142748', 10, 2 );
mujuonly
  • 11,370
  • 5
  • 45
  • 75
Vignesh Pichamani
  • 7,950
  • 22
  • 77
  • 115
  • 1
    Oh, I see the mistake. My code is not meant to be inside the theme template. Make a small plugin for it, refer to [this Answer](http://wordpress.stackexchange.com/a/72183/12615) on how to proceed. Maybe you can clean the Question, the template is not relevant. – brasofilo May 15 '13 at 05:58

1 Answers1

2

There's a useful filter in templates/myaccount/my-downloads.php.

It needs a bit of contortion, but it's possible to get the filename. Check comments:

<?php
/**
 * Plugin Name: Add Filenames to WooCommerce Digital Downloads
 * Plugin URI:  http://stackoverflow.com/questions/16142748/
 * Author:      brasofilo
 * Author URI:  http://wordpress.stackexchange.com/users/12615/brasofilo
 */

add_filter( 'woocommerce_available_download_link', 'filter_wc_downloads_so_16142748', 10, 2 );

function filter_wc_downloads_so_16142748( $link, $download )
{
    // Create a WC_Order object and get the file URLs for this product
    $order = new WC_Order( $download['order_id'] );
    $download_file_urls = $order->get_downloadable_file_urls( 
        $download['product_id'], 
        null, 
        $download['download_id'] 
    );

    // Check each download URL and compare with the current URL 
    // $key contains the real file URL and $value is the encoded URL
    foreach( $download_file_urls as $key => $value )
    {
        if( $value == $download['download_url'] )
        {
            $url_parts = explode( '/', parse_url( $key, PHP_URL_PATH ) );
            $file_name = end( $url_parts );
            $link = '<a href="' 
                . esc_url( $download['download_url'] ) 
                . '">' 
                . $download['download_name'] 
                . '</a> <small>( ' 
                . $file_name 
                . ' )</small>';
        }               
    }
    return $link;
}

Results in:

woocommerce downloads with file name


The $download var contains this:

array ( 
    'download_url' => 'http://example.com/?download_file=657&order=order_5192e53&email=user@email.com&key=49d12', 
    'download_id' => '49d1232377ed1752d5622b63786d089b', 
    'product_id' => '657', 
    'download_name' => 'Product title — File 1', 
    'order_id' => 659, 
    'order_key' => 'order_5192e536baf27', 
    'downloads_remaining' => '', 
)

This seems a nice feature to have and I'll probably add this to a site I'm working.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
  • Hi Brasofilo I used that code but there is nothing changes in my-account page. If You Proceed I can paste that code too....... Thanks – Vignesh Pichamani May 15 '13 at 05:25
  • 1
    What WC version are you using? And yes, if you think it's relevant, post it. – brasofilo May 15 '13 at 05:46
  • 1
    Ok, I've added the plugin header to my code above (I should consider doing it always). Create a new PHP file with it and drop it in the `/plugins` folder, then activate in the Dashboard. – brasofilo May 15 '13 at 06:04
  • Thanks,I have Activated that Plugin. Then What shall i do.? I saw in the my account page Still nothing happen. – Vignesh Pichamani May 15 '13 at 06:11
  • 1
    I'm not really sure... I downloaded v2.0.4 and it works with it too in my test environment (I had 2.0.9). Is your plugin clean of modifications or hacks? Can you reinstall it? Case you don't want to upgrade, you can get the same version you're using in this page: http://wordpress.org/extend/plugins/woocommerce/developers/ – brasofilo May 15 '13 at 06:19
  • Thanks, I used that version 2.0.4 from the woocommerce developers link that you provide. I replace that plugin and add that plugin its working fine. I think its should be the modification in that plugin. Thanks and great – Vignesh Pichamani May 15 '13 at 06:33