Add total customer orders counter column to admin orders list in WooCommerce

/**
 * Add "Orders" column
 */
add_filter( 'manage_edit-shop_order_columns', 'add_shop_order_column_completed_orders', PHP_INT_MAX );
function add_shop_order_column_completed_orders( $columns ) {
	$columns['completed_orders'] = 'Orders';
	return $columns;
}
/**
 * Output "Orders" column
 */
add_action( 'manage_shop_order_posts_custom_column', 'render_shop_order_column_completed_orders', PHP_INT_MAX );
function render_shop_order_column_completed_orders( $column ) {
	if ( 'completed_orders' != $column ) {
		return;
	}
	$_order = wc_get_order( get_the_ID() );
	if ( 'completed' === $_order->get_status() ) {
		if ( 0 != $_order->customer_user ) {
			$customer_orders = get_posts( array(
				'numberposts' => -1,
				'meta_key'    => '_customer_user',
				'meta_value'  => $_order->customer_user,
				'post_type'   => wc_get_order_types(),
				'post_status' => array( 'wc-completed' ), // for all orders use: array_keys( wc_get_order_statuses() )
			) );
			$info = '<a href="/wp-admin/edit.php?post_status=wc-completed&post_type=shop_order&_customer_user='
				. $_order->customer_user . '">' . count( $customer_orders ) . '</a>';
			wp_reset_postdata();
		} else {
			$info = '[guest]';
		}
		echo $info;
	}
}

No comments yet.

Leave a Reply