WooCommerce我的帐户-下载:列出所有产品 [英] WooCommerce my account - downloads: listing all products
本文介绍了WooCommerce我的帐户-下载:列出所有产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的网站上,我仅提供虚拟产品,这就是为什么我要在我的帐户中进行更改-下载视图.
On my website I only offer virtual products, that's why I want to make changes in the my account - download view.
我正在使用以下代码.
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;
}
echo '<ul class="charliesub">';
echo '<li class="accordion">';
echo '<h3>' . $order_date . '</h3>';
echo '<div>';
echo '<ol>';
foreach ( $lists as $list ) {
// Get $order object from order ID
$order = wc_get_order( $list['orderid'] );
$order_date = $order->get_date_completed();
echo '<li>';
echo '<a href="' . esc_url( $list['download_url'] ) . '" class="woocommerce-MyAccount-downloads-file">';
echo esc_html( $list['file_name'] );
echo '</a></li>';
}
echo '</ol>';
echo '</div>';
echo '</li>';
echo '</ul>';
}
add_action( 'woocommerce_account_downloads_column_download-file', 'prefix_downloads_column_download_file' );
但是,订单的日期显示在每个链接的上方下载链接.
However, the date of the order is showing above each download link.
我只需要将日期放在所有链接的上方 .(放在我想要的< h3>
标记中).
I would need to only have the date once above all the links. (placed like I want in the <h3>
tag).
有人可以帮助我进一步进行调整吗?
Someone who can help me further with the adjustments already made?
推荐答案
在foreach循环中,您可以将值存储在变量中,而不是打印(回显).
In the foreach loop you can store the values in a variable instead of printing (echo).
在foreach之后,您可以打印变量
After the foreach you can then print the variable
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_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', 10, 1 );
/**
* 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;
}
// Set variable
$li = '';
foreach ( $lists as $list ) {
// Get $order object from order ID
$order = wc_get_order( $list['orderid'] );
$order_date = $order->get_date_completed();
// Append to
$li .= '<li><a href="' . esc_url( $list['download_url'] ) . '" class="woocommerce-MyAccount-downloads-file">' . esc_html( $list['file_name'] ) . '</a></li>';
}
echo '<ul class="charliesub">';
echo '<li class="accordion">';
echo '<h3>' . $order_date . '</h3>';
echo '<div>';
echo '<ol>';
echo $li;
echo '</ol>';
echo '</div>';
echo '</li>';
echo '</ul>';
}
add_action( 'woocommerce_account_downloads_column_download-file', 'prefix_downloads_column_download_file' );
这篇关于WooCommerce我的帐户-下载:列出所有产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文