在 Woocommerce 中获取客户 "on-hold" 订单状态总金额

Get customer "on-hold" order status total amount in Woocommerce

我正在尝试让 woocommerce 中的用户暂停所有产品的价格(即用户已下订单但尚未付款)。

我有以下代码检测用户的所有产品暂停订单

function get_user_on_hold_product_price() {

    global $product, $woocommerce;

    // GET USER
    $current_user = wp_get_current_user();

    // GET USER ON-HOLD ORDERS
    $customer_orders = get_posts( array(
        'numberposts' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => $current_user->ID,
        'post_type'   => 'shop_order',
        'post_status' => 'wc-on-hold',
    ) );

我不确定从这里该怎么做才能仅获得用户所有暂停订单的总价。

Adding/hooking这个函数变成简码,像这样;

add_shortcode('get_on-hold_price', 'get_user_on_hold_product_price')

谢谢

要使用 WC_Order_Query 获取客户 "on-hold" 订单的总数以提高可用性和兼容性:

add_shortcode('user_on_hold_total', 'get_user_orders_on_hold_total');
function get_user_orders_on_hold_total() {
    $total_amount = 0; // Initializing

    // Get current user
    if( $user = wp_get_current_user() ){

        // Get 'on-hold' customer ORDERS
        $on_hold_orders = wc_get_orders( array(
            'limit' => -1,
            'customer_id' => $user->ID,
            'status' => 'on-hold',
        ) );

        foreach( $on_hold_orders as $order) {
            $total_amount += $order->get_total();
        }
    }
    return $total_amount;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。已测试并有效。

要获得格式化的总金额,请将 return $total_amount; 替换为 return wc_price($total_amount);

Shortcode usage: [user_on_hold_total]

相关文档:Woocommerce wc_get_orders() and WC_Order_Query