从订单处理和新购物车订单中获取剩余订单(差异)

时间:2017-01-11 09:05:51

标签: php wordpress woocommerce cart orders

在我的Woocommerce网站上,我的最大订单容量为50.

在我们关闭订购之前,我正试图在购物车中与客户沟通。

我需要获得每个订单中已处理的商品总数+购物车中的新订单从最大订单数减去。

我已尝试使用此代码:

function display_woocommerce_order_count() {

global $woocommerce; 

$args = array(
    'post_type'         => 'shop_order',
    'post_status'       => 'publish',
    'posts_per_page'    => -1,
    'tax_query'         => array(
                                    array(
                                        'taxonomy' => 'shop_order_status',
                                        'field' => 'slug',
                                        'terms' => array('processing')
                                    )
                                )
           );

    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();

    $order_id = $loop->post->ID;
    $order = new WC_Order($order_id);

    $order_count = 0;
    foreach( $order as $product ) {
        $order_item = $product['qty'];
        if($qty) {
            $order_count += $order_item;
        }
    }

    ob_start(); 

    //Echo the number of items in cart.
    $count = $woocommerce->cart->cart_contents_count; 

    //Difference max - orders processing - cart items
    $total_diff = 50 - number_format($order_count) - $count;

    echo $total_diff;

    return ob_get_clean();
}

如何使其按预期工作?

由于

1 个答案:

答案 0 :(得分:1)

从现有客户那里获得剩余的订单计算"处理"订购商品和实际购物车商品,您可以尝试使用此自定义功能(带有可选的 $user_id 参数)。

这是代码:

function get_remaining_orders( $user_id = null ){

    if( empty($user_id) && is_user_logged_in() )
        $user_id = get_current_user_id();

    if( ! empty($user_id) && ! is_admin() ){

        $order_max = 50;
        $processing_orders_items_count = 0;
        $cart_count = 0;

        $customer_orders = get_posts( array(
            'meta_key' => '_customer_user',
            'meta_value' => $user_id,
            'post_type'   => 'shop_order',
            'numberposts' => -1,
            'post_status' => 'wc-processing' // 'processing' order status only
        ) );

        if(!empty($customer_orders))
            foreach($customer_orders as $customer_order_values){
                $customer_order = wc_get_order( $customer_order_values->ID );
                $processing_orders_items_count += $customer_order->get_item_count('line_item');
            }

        if(!WC()->cart->is_empty())
            $cart_count = WC()->cart->get_cart_contents_count( );

        $ouput = $order_max - ($processing_orders_items_count + $cart_count);

        return $ouput;
    }
}

// USAGE: for a specific user ID (here for example $user_id is 22):
get_remaining_orders( 22 );

// USAGE: returning the value in a variable for current user:
$remaining_orders = get_remaining_orders();

// USAGE: displaying the value for current user (example):
echo 'Total remaining orders are ' . get_remaining_orders();

代码放在活动子主题(或主题)的function.php文件中。或者也可以在任何插件php文件中。

此代码经过测试且功能正常。

  

更新"非用户特定案例"

function get_remaining_orders(){

    if( !is_admin() ){

        $order_max = 50;
        $processing_orders_items_count = 0;
        $cart_count = 0;

        $customer_orders = get_posts( array(
            'post_type'   => 'shop_order',
            'numberposts' => -1,
            'post_status' => 'wc-processing' // 'processing' order status only
        ) );

        if(!empty($customer_orders))
            foreach($customer_orders as $customer_order_values){
                $customer_order = wc_get_order( $customer_order_values->ID );
                $processing_orders_items_count += $customer_order->get_item_count('line_item');
            }

        if(!WC()->cart->is_empty())
            $cart_count = WC()->cart->get_cart_contents_count( );

        $ouput = $order_max - ($processing_orders_items_count + $cart_count);

        return $ouput;
    }
}

// USAGE: returning the value in a variable:
$remaining_orders = get_remaining_orders();

// USAGE: displaying the value (example):
echo 'Total remaining orders are ' . get_remaining_orders();

这应该像你期望的那样工作......