在WooCommerce&Quot;My Account&Quot;Orders表的新列中显示产品类别 [英] Show product categories in a new column on WooCommerce "My account" orders table
本文介绍了在WooCommerce&Quot;My Account&Quot;Orders表的新列中显示产品类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想添加一个定制列,以便在WooCommerce的订单历史记录表中显示产品类别
我找到了如何添加自定义列,但似乎无法在此列中显示链接到订单的分类产品。
对于此示例,我只有一个产品,但如果我可以显示多个税,那就更好了。
这是我找到的(来自:Skyverge博客)来添加一个新栏目:
/**
* Adds a new column to the "My Orders" table in the account.
*
* @param string[] $columns the columns in the orders table
* @return string[] updated columns
*/
function sv_wc_add_my_account_orders_column( $columns ) {
$new_columns = array();
foreach ( $columns as $key => $name ) {
$new_columns[ $key ] = $name;
// add ship-to after order status column
if ( 'order-number' === $key ) {
$new_columns['order-ship-to'] = __( 'Catégorie', 'textdomain' );
}
}
return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'sv_wc_add_my_account_orders_column' );
欢迎任何指针
推荐答案
使用当前代码,您可以在现有列之间添加一列,但是:
- 自WooCommerce 2.6.0起,
woocommerce_my_account_my_orders_columns
筛选器已弃用。并替换为woocommerce_account_orders_columns
- 缺少要在栏中添加内容的部分
$column_id
需要替换为order-category
因此您得到:
// Adds a new column to the "My Orders" table in the account.
function filter_woocommerce_account_orders_columns( $columns ) {
$new_columns = array();
foreach ( $columns as $key => $column ) {
$new_columns[ $key ] = $column;
// Add after order number column
if ( $key === 'order-number' ) {
$new_columns['order-category'] = __( 'Catégorie', 'woocommerce' );
}
}
return $new_columns;
}
add_filter( 'woocommerce_account_orders_columns', 'filter_woocommerce_account_orders_columns', 10, 1 );
// Adds data to the custom "order-category" column in "My Account > Orders"
function filter_woocommerce_my_account_my_orders_column_order( $order ) {
// Initialize
$categories = array();
// Loop through order items
foreach ( $order->get_items() as $item_key => $item ) {
// Get product ID
$product_id = $item->get_product_id();
// Get terms
$term_names = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'names' ) );
// Loop through term names
foreach ( $term_names as $term_name ) {
// NOT in array
if ( ! in_array( $term_name, $categories, true ) ) {
// Push one or more elements onto the end of array
array_push( $categories, $term_name );
}
}
}
// NOT empty
if ( ! empty( $categories ) ) {
echo implode( ', ', $categories );
}
}
add_action( 'woocommerce_my_account_my_orders_column_order-category', 'filter_woocommerce_my_account_my_orders_column_order', 10, 1 );
这篇关于在WooCommerce&Quot;My Account&Quot;Orders表的新列中显示产品类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文