如何在 WooCommerce 中列出每个可下载产品的每个文件名?

Vig*_*ani 5 php wordpress woocommerce woothemes

我正在使用 WooCommerce 插件。在可下载的产品中,如何显示文件名而不是产品名称?现在可下载产品在“我的帐户”页面中如下所示:

title-->file1
title-->file2. 
Run Code Online (Sandbox Code Playgroud)

现在我需要将该文件列表显示为文件名。例如:在“订单”页面中,它应该是这样的:

title-->Installer.zip(filename)
title-->Runner.zip(filename)
Run Code Online (Sandbox Code Playgroud)

如何在可下载产品列表中列出每个文件名?

我正在使用 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 );
Run Code Online (Sandbox Code Playgroud)

bra*_*ilo 2

有一个有用的过滤器templates/myaccount/my-downloads.php

\n\n

它需要一些变形,但可以获得文件名。检查评论:

\n\n
<?php\n/**\n * Plugin Name: Add Filenames to WooCommerce Digital Downloads\n * Plugin URI:  http://stackoverflow.com/questions/16142748/\n * Author:      brasofilo\n * Author URI:  http://wordpress.stackexchange.com/users/12615/brasofilo\n */\n\nadd_filter( \'woocommerce_available_download_link\', \'filter_wc_downloads_so_16142748\', 10, 2 );\n\nfunction filter_wc_downloads_so_16142748( $link, $download )\n{\n    // Create a WC_Order object and get the file URLs for this product\n    $order = new WC_Order( $download[\'order_id\'] );\n    $download_file_urls = $order->get_downloadable_file_urls( \n        $download[\'product_id\'], \n        null, \n        $download[\'download_id\'] \n    );\n\n    // Check each download URL and compare with the current URL \n    // $key contains the real file URL and $value is the encoded URL\n    foreach( $download_file_urls as $key => $value )\n    {\n        if( $value == $download[\'download_url\'] )\n        {\n            $url_parts = explode( \'/\', parse_url( $key, PHP_URL_PATH ) );\n            $file_name = end( $url_parts );\n            $link = \'<a href="\' \n                . esc_url( $download[\'download_url\'] ) \n                . \'">\' \n                . $download[\'download_name\'] \n                . \'</a> <small>( \' \n                . $file_name \n                . \' )</small>\';\n        }               \n    }\n    return $link;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

结果是:

\n\n

woocommerce 下载文件名

\n\n
\n\n

$download包含这个:

\n\n
array ( \n    \'download_url\' => \'http://example.com/?download_file=657&order=order_5192e53&email=user@email.com&key=49d12\', \n    \'download_id\' => \'49d1232377ed1752d5622b63786d089b\', \n    \'product_id\' => \'657\', \n    \'download_name\' => \'Product title \xe2\x80\x94 File 1\', \n    \'order_id\' => 659, \n    \'order_key\' => \'order_5192e536baf27\', \n    \'downloads_remaining\' => \'\', \n)\n
Run Code Online (Sandbox Code Playgroud)\n\n

这似乎是一个不错的功能,我可能会将其添加到我正在工作的网站中。

\n