【问题标题】:Display customer cancelled orders count in Woocommerce Admin Edit Orders pages在 Woocommerce 管理员编辑订单页面中显示客户取消的订单数量
【发布时间】:2018-05-16 08:56:10
【问题描述】:

我正在尝试计算客户已取消的订单数量并将其显示在管理订单屏幕中。

我的问题是我不能让它为远程客户工作,我可以让它为我自己工作(作为 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 ); 

非常感谢任何帮助!

【问题讨论】:

    标签: php wordpress woocommerce backend orders


    【解决方案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 文件或任何插件文件中。

    经过测试并且有效。

    【讨论】:

    • 非常感谢,就像一个魅力!确实,您的方式更加简单明了!
    【解决方案2】:

    我将它用于我的项目:
    使用

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

    最后使用这个短代码:

    [地理获取客户订单]

    【讨论】:

    • 您能解释一下为什么您认为这些更改是必要的吗?
    猜你喜欢
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-25
    • 1970-01-01
    • 2020-10-29
    • 1970-01-01
    相关资源
    最近更新 更多