在Woocommerce订单管理列表中显示用户失败和取消的订单计数



>我已经将列添加到订单表中(是这样称呼的(,我现在尝试首先计算用户有多少(如果有的话(失败和取消的订单,然后使用 echo 显示该数量。这是我的代码,它显示了列,但显示了一个空白列。

任何帮助都非常感谢。

代码:

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>';
}
}

代码进入函数.php活动子主题(或活动主题(的文件。经过测试并工作。

最新更新