如何在wordpress中管理列,以便在woocommerce订单面板中添加新列获取产品类别。我已经找到了在整个面板中创建新列的方法,但我不知道如何添加。
//MANAGE COLUMNS
add_filter( 'manage_pagamento_posts_columns', 'green_filter_posts_columns' );
function green_filter_posts_columns( $columns ) {
$columns = array(
'cb' => $columns['cb'],
'title' => __( 'Title' ),
'pacote' => __( 'Pacote', 'green' ),
'temporada' => __( 'Temporada', 'green' )
);
return $columns;
}
答案 0 :(得分:0)
我找到了一种在woocommerce订单面板中添加列的方法。在此代码中,我将展示如何在按产品过滤的每个订单行中添加产品类别,并在发生时删除重复的类别。
// ADDING A NEW COLUMN (keeping "Total" and "Actions" columns at the end)
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 20 );
function custom_shop_order_column($columns)
{
$reordered_columns = array();
// Inserting columns to a specific location
foreach( $columns as $key => $column){
$reordered_columns[$key] = $column;
if( $key == 'order_status' ){
// Inserting after "Status" column
$reordered_columns['my-column1'] = __( 'Categorias','theme_domain');
}
}
return $reordered_columns;
}
// Adding custom fields meta data for each new column (example)
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 20, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
switch ( $column )
{
case 'my-column1' :
$cats = '';
$cats_final = '';
$item_cats = array();
$order = wc_get_order( $post_id );
$order_data = $order->get_data();
foreach ($order->get_items() as $item_key => $item_values):
## Using WC_Order_Item methods ##
// Item ID is directly accessible from the $item_key in the foreach loop or
$item_id = $item_values->get_product_id();
$item_cat = get_the_terms($item_id, 'product_cat');
$item_cat = array_unique($item_cat);
foreach ( $item_cat as $term ) {
array_push($item_cats, $term->name);
}
endforeach;
$item_cats = array_unique($item_cats);
foreach($item_cats as $item_final){
$cats_final .= $item_final.', ';
}
echo rtrim($cats_final,", ");
break;
}
}
答案 1 :(得分:0)
我无法在上面的答案中添加评论,所以我发布了一个新评论。来自doughfabris的答案几乎奏效。我在开发人员网站上进行了测试,发现代码在删除后可以使用“ $item_cats = array_unique($item_cats);
” 在下方“ $order_data = $order->get_data();
”
这里的代码已经过测试,对我来说效果很好。谢谢dougfabris! 顺便说一句,如何使其仅显示最高级别?
// ADDING A NEW COLUMN (keeping "Total" and "Actions" columns at the end)
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 20 );
function custom_shop_order_column($columns)
{
$reordered_columns = array();
// Inserting columns to a specific location
foreach( $columns as $key => $column){
$reordered_columns[$key] = $column;
if( $key == 'order_status' ){
// Inserting after "Status" column
$reordered_columns['my-column1'] = __( 'Categorias','theme_domain');
}
}
return $reordered_columns;
}
// Adding custom fields meta data for each new column (example)
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 20, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
switch ( $column )
{
case 'my-column1' :
$cats = '';
$cats_final = '';
$item_cats = array();
$order = wc_get_order( $post_id );
$order_data = $order->get_data();
foreach ($order->get_items() as $item_key => $item_values):
## Using WC_Order_Item methods ##
// Item ID is directly accessible from the $item_key in the foreach loop or
$item_id = $item_values->get_product_id();
$item_cat = get_the_terms($item_id, 'product_cat');
foreach ( $item_cat as $term ) {
array_push($item_cats, $term->name);
}
endforeach;
$item_cats = array_unique($item_cats);
foreach($item_cats as $item_final){
$cats_final .= $item_final.', ';
}
echo rtrim($cats_final,", ");
break;
}
}