在 Woocommerce 管理员编辑订单页面中显示客户取消的订单数
Display customer cancelled orders count in Woocommerce Admin Edit Orders pages
我正在计算客户取消的订单数量,并在管理订单屏幕中显示它们。
我的问题是我无法让它为远程客户工作,我可以让它为我自己工作(如 current_user)。
这是我的代码(取自其他谷歌搜索和一些小修改):
function count_order_no( $atts, $content = null ) {
$args = shortcode_atts( array(
'status' => 'cancelled',
), $atts );
$statuses = array_map( 'trim', explode( ',', $args['status'] ) );
$order_count = 0;
foreach ( $statuses as $status ) {
// if we didn't get a wc- prefix, add one
if ( 0 !== strpos( $status, 'wc-' ) ) {
$status = 'wc-' . $status;
}
$order_count += wp_count_posts( 'shop_order' )->$status;
}
ob_start();
echo number_format( $order_count );
return ob_get_clean();
}
add_shortcode( 'wc_order_count', 'count_order_no' );
然后在admin中显示号码
// print the number
function print_the_number() {
echo do_shortcode( '[wc_order_count]' );
}
// add the action
add_action( 'woocommerce_admin_order_data_after_order_details', 'print_the_number', 10, 1 );
非常感谢任何帮助!
您需要定位当前订单中的客户 ID。它可以以更简单的方式完成。
你应该试试这个:
add_action( 'woocommerce_admin_order_data_after_order_details', 'get_specific_customer_orders', 10, 1 );
function get_specific_customer_orders( $order ) {
$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'),
) );
$orders_count = '<strong style="color:#ca4a1f">' . count($customer_orders) . '</strong>';
echo'<br clear="all">
<p>' . __( 'Cancelled orders count: ' ) . $orders_count . '</p>';
}
代码进入您的活动子主题(或活动主题)的 function.php 文件或任何插件文件。
已测试并有效。
我将其用于我的项目:
使用
get_current_user_id()
而不是
$order->get_customer_id()
add_shortcode( 'geo-get-customer-orders', 'geo_get_customer_orders' );
function geo_get_customer_orders() {
global $order;
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order',
'post_status' => array('wc-completed'),
) );
echo count($customer_orders);
}
终于使用这个简码了:
[geo-get-customer-orders]
我正在计算客户取消的订单数量,并在管理订单屏幕中显示它们。
我的问题是我无法让它为远程客户工作,我可以让它为我自己工作(如 current_user)。
这是我的代码(取自其他谷歌搜索和一些小修改):
function count_order_no( $atts, $content = null ) {
$args = shortcode_atts( array(
'status' => 'cancelled',
), $atts );
$statuses = array_map( 'trim', explode( ',', $args['status'] ) );
$order_count = 0;
foreach ( $statuses as $status ) {
// if we didn't get a wc- prefix, add one
if ( 0 !== strpos( $status, 'wc-' ) ) {
$status = 'wc-' . $status;
}
$order_count += wp_count_posts( 'shop_order' )->$status;
}
ob_start();
echo number_format( $order_count );
return ob_get_clean();
}
add_shortcode( 'wc_order_count', 'count_order_no' );
然后在admin中显示号码
// print the number
function print_the_number() {
echo do_shortcode( '[wc_order_count]' );
}
// add the action
add_action( 'woocommerce_admin_order_data_after_order_details', 'print_the_number', 10, 1 );
非常感谢任何帮助!
您需要定位当前订单中的客户 ID。它可以以更简单的方式完成。
你应该试试这个:
add_action( 'woocommerce_admin_order_data_after_order_details', 'get_specific_customer_orders', 10, 1 );
function get_specific_customer_orders( $order ) {
$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'),
) );
$orders_count = '<strong style="color:#ca4a1f">' . count($customer_orders) . '</strong>';
echo'<br clear="all">
<p>' . __( 'Cancelled orders count: ' ) . $orders_count . '</p>';
}
代码进入您的活动子主题(或活动主题)的 function.php 文件或任何插件文件。
已测试并有效。
我将其用于我的项目:
使用
get_current_user_id()
而不是
$order->get_customer_id()
add_shortcode( 'geo-get-customer-orders', 'geo_get_customer_orders' );
function geo_get_customer_orders() {
global $order;
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => 'shop_order',
'post_status' => array('wc-completed'),
) );
echo count($customer_orders);
}
终于使用这个简码了:
[geo-get-customer-orders]