WooCommerce 我的帐户 - 下载:列出所有产品

在我的网站上,我只提供虚拟产品,这就是为什么我想在我的帐户中进行更改 - 下载视图。


我正在使用以下代码。


function filter_woocommerce_customer_available_downloads($downloads, $customer_id) { 


    // Manipulate download data here, this example we'll get the first 5 downloads in the array

    // $downloads = array_slice($downloads, 0, 5);

 // Sorting the array by Order Ids in DESC order

        arsort($downloads); 


    // Return first five downloads

    return $downloads;


}; 

// Add the filter, this tells wordpress to apply this filter every time available downloads is called

add_filter( 'woocommerce_customer_available_downloads', 'filter_woocommerce_customer_available_downloads', 10, 2 );


/**

 * Group Downloadable products by product ID

 *

 * @param array $downloads

 * @return array

 */

function prefix_group_downloadable_products( array $downloads ) {

    $unique_downloads = [];


    foreach ($downloads as $download ) {

        $list = [

            'download_url' => $download['download_url'],

            'file_name'    => $download['file']['name'],

            'orderid'  => $download['order_id']

        ];



        if ( array_key_exists( $download['product_id'], $unique_downloads ) ) {

            $unique_downloads[ $download['product_id'] ]['list'][] = $list;

            continue;

        }




        $data = $download;

        $data['list'] = [ $list ];

        $unique_downloads[ $download['product_id'] ] = $data;

    }


    return $unique_downloads;

}


add_filter( 'woocommerce_customer_get_downloadable_products', 

'prefix_group_downloadable_products' );


/**

 * Show number list of downloadable files for group product

 *

 * @param array $download

 * @return void

 */


function prefix_downloads_column_download_file( array $download ) {



    $lists = $download['list'];


            // Sorting the array by Order Ids in DESC order

        arsort($lists); 


    if ( empty( $lists ) ) {

        _e( 'No Download Files', 'storefront' );

        return;

    }


    }


但是,订单日期显示在每个下载链接上方。


我只需要在所有链接上都有日期。(按照我想要的方式放置在<h3>标签中)。


有人可以帮助我进一步进行已经进行的调整吗?


慕婉清6462132
浏览 141回答 1
1回答

偶然的你

在 foreach 循环中,您可以将值存储在变量中,而不是打印(回显)。在 foreach 之后,您可以打印变量function filter_woocommerce_customer_available_downloads($downloads, $customer_id) {&nbsp;&nbsp; &nbsp; // Manipulate download data here, this example we'll get the first 5 downloads in the array&nbsp; &nbsp; // $downloads = array_slice($downloads, 0, 5);&nbsp; &nbsp; // Sorting the array by Order Ids in DESC order&nbsp; &nbsp; arsort($downloads);&nbsp;&nbsp; &nbsp; // Return first five downloads&nbsp; &nbsp; return $downloads;}add_filter( 'woocommerce_customer_available_downloads', 'filter_woocommerce_customer_available_downloads', 10, 2 );/**&nbsp;* Group Downloadable products by product ID&nbsp;*&nbsp;* @param array $downloads&nbsp;* @return array&nbsp;*/function prefix_group_downloadable_products( array $downloads ) {&nbsp; &nbsp; $unique_downloads = [];&nbsp; &nbsp; foreach ($downloads as $download ) {&nbsp; &nbsp; &nbsp; &nbsp; $list = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'download_url' => $download['download_url'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'file_name'&nbsp; &nbsp; => $download['file']['name'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'orderid'&nbsp; &nbsp; &nbsp; => $download['order_id']&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; if ( array_key_exists( $download['product_id'], $unique_downloads ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $unique_downloads[ $download['product_id'] ]['list'][] = $list;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $data = $download;&nbsp; &nbsp; &nbsp; &nbsp; $data['list'] = [ $list ];&nbsp; &nbsp; &nbsp; &nbsp; $unique_downloads[ $download['product_id'] ] = $data;&nbsp; &nbsp; }&nbsp; &nbsp; return $unique_downloads;}add_filter( 'woocommerce_customer_get_downloadable_products', 'prefix_group_downloadable_products', 10, 1 );/**&nbsp;* Show number list of downloadable files for group product&nbsp;*&nbsp;* @param array $download&nbsp;* @return void&nbsp;*/function prefix_downloads_column_download_file( array $download ) {&nbsp; &nbsp; $lists = $download['list'];&nbsp; &nbsp; // Sorting the array by Order Ids in DESC order&nbsp; &nbsp; arsort($lists);&nbsp;&nbsp; &nbsp; if ( empty( $lists ) ) {&nbsp; &nbsp; &nbsp; &nbsp; _e( 'No Download Files', 'storefront' );&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; // Set variable&nbsp; &nbsp; $li = '';&nbsp; &nbsp; foreach ( $lists as $list ) {&nbsp; &nbsp; &nbsp; &nbsp; // Get $order object from order ID&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $order = wc_get_order( $list['orderid']&nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; $order_date = $order->get_date_completed();&nbsp; &nbsp; &nbsp; &nbsp; // Append to&nbsp; &nbsp; &nbsp; &nbsp; $li .= '<li><a href="' . esc_url( $list['download_url'] ) . '" class="woocommerce-MyAccount-downloads-file">' . esc_html( $list['file_name'] ) . '</a></li>';&nbsp; &nbsp; }&nbsp; &nbsp; echo '<ul class="charliesub">';&nbsp; &nbsp; echo '<li class="accordion">';&nbsp; &nbsp; echo '<h3>' .&nbsp; $order_date . '</h3>';&nbsp; &nbsp; echo '<div>';&nbsp; &nbsp; echo '<ol>';&nbsp; &nbsp; echo $li;&nbsp; &nbsp; echo '</ol>';&nbsp; &nbsp; echo '</div>';&nbsp; &nbsp; echo '</li>';&nbsp; &nbsp; echo '</ul>';}add_action( 'woocommerce_account_downloads_column_download-file', 'prefix_downloads_column_download_file' );
打开App,查看更多内容
随时随地看视频慕课网APP