在 Woocommerce Orders Admin 列表中显示用户失败和取消的订单数
Display in Woocommerce Orders Admin list users failed and cancelled orders count
我已将该列添加到订单 table(是这样叫的吗)我现在尝试首先计算用户有多少失败和取消的订单,然后显示使用回声的那个数量。这是我的代码,它显示了该列但显示了一个空白列。
非常感谢任何帮助。
代码:
function add_failed_orders_column_to_order_page( $columns ) {
$new_columns = array();
foreach ( $columns as $column_name => $column_info ) {
$new_columns[ $column_name ] = $column_info;
if ( 'order_total' === $column_name ) {
$new_columns['previous_failed_customer_orders'] = __( 'Failed Orders', 'ocean-child' );
} }
return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'add_failed_orders_column_to_order_page', 20 );
add_action( 'manage_shop_order_posts_custom_column' , 'display_previous_failed_orders', 10, 2 );
function display_previous_failed_orders( $order, $column )
{
global $order;
switch ( $column )
{
case 'previous_failed_customer_orders' :
$failed_customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $order->get_customer_id(),
'post_type' => 'shop_order',
'post_status' => array('wc-cancelled', 'wc-failed'),
) );
$failed_orders_count = '<strong style="color:red !important; font-size:15px !important;">' . count($failed_customer_orders) . '</strong>'; {
echo $failed_orders_count;
}
break;
}
}
下面的代码使用一个非常简单且轻量级的 SQL 查询来获取处于已取消和失败状态的用户订单的数量。您的代码中也有一些错误。
您重访的代码:
add_filter( 'manage_edit-shop_order_columns', 'add_column_user_failled_cancelled', 20, 1 );
function add_column_user_failled_cancelled( $columns ) {
$new_columns = array();
foreach ( $columns as $column_key => $column_label ) {
$new_columns[$column_key] = $column_label;
if ( 'order_total' === $column_key ) {
$new_columns['cancelled_failled'] = __( 'Failed Orders', 'ocean-child' );
}
}
return $new_columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'column_content_user_failled_cancelled', 10, 1 );
function column_content_user_failled_cancelled( $column ) {
global $post, $the_order, $wpdb;
if ( $column =='cancelled_failled' ) {
// Get the count of the user failled and cancelled orders
$count = $wpdb->get_var( "
SELECT COUNT(p.ID) FROM {$wpdb->prefix}posts AS p
JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
WHERE p.post_type = 'shop_order' AND p.post_status IN ('wc-cancelled','wc-failed')
AND pm.meta_key = '_customer_user' AND pm.meta_value = '{$the_order->get_customer_id()}'
");
echo '<strong style="color:red !important; font-size:15px !important;">' . $count . '</strong>';
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。
我已将该列添加到订单 table(是这样叫的吗)我现在尝试首先计算用户有多少失败和取消的订单,然后显示使用回声的那个数量。这是我的代码,它显示了该列但显示了一个空白列。
非常感谢任何帮助。
代码:
function add_failed_orders_column_to_order_page( $columns ) {
$new_columns = array();
foreach ( $columns as $column_name => $column_info ) {
$new_columns[ $column_name ] = $column_info;
if ( 'order_total' === $column_name ) {
$new_columns['previous_failed_customer_orders'] = __( 'Failed Orders', 'ocean-child' );
} }
return $new_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'add_failed_orders_column_to_order_page', 20 );
add_action( 'manage_shop_order_posts_custom_column' , 'display_previous_failed_orders', 10, 2 );
function display_previous_failed_orders( $order, $column )
{
global $order;
switch ( $column )
{
case 'previous_failed_customer_orders' :
$failed_customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $order->get_customer_id(),
'post_type' => 'shop_order',
'post_status' => array('wc-cancelled', 'wc-failed'),
) );
$failed_orders_count = '<strong style="color:red !important; font-size:15px !important;">' . count($failed_customer_orders) . '</strong>'; {
echo $failed_orders_count;
}
break;
}
}
下面的代码使用一个非常简单且轻量级的 SQL 查询来获取处于已取消和失败状态的用户订单的数量。您的代码中也有一些错误。
您重访的代码:
add_filter( 'manage_edit-shop_order_columns', 'add_column_user_failled_cancelled', 20, 1 );
function add_column_user_failled_cancelled( $columns ) {
$new_columns = array();
foreach ( $columns as $column_key => $column_label ) {
$new_columns[$column_key] = $column_label;
if ( 'order_total' === $column_key ) {
$new_columns['cancelled_failled'] = __( 'Failed Orders', 'ocean-child' );
}
}
return $new_columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'column_content_user_failled_cancelled', 10, 1 );
function column_content_user_failled_cancelled( $column ) {
global $post, $the_order, $wpdb;
if ( $column =='cancelled_failled' ) {
// Get the count of the user failled and cancelled orders
$count = $wpdb->get_var( "
SELECT COUNT(p.ID) FROM {$wpdb->prefix}posts AS p
JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
WHERE p.post_type = 'shop_order' AND p.post_status IN ('wc-cancelled','wc-failed')
AND pm.meta_key = '_customer_user' AND pm.meta_value = '{$the_order->get_customer_id()}'
");
echo '<strong style="color:red !important; font-size:15px !important;">' . $count . '</strong>';
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。