簡體   English   中英

在 Woocommerce Admin Edit Orders 頁面中顯示客戶取消的訂單數

[英]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' );

然后在管理員中顯示號碼

// 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]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM