omukiguy
omukiguy

Reputation: 1437

Get remaining orders(difference) from orders processing and new cart orders

In my Woocommerce website, I have a maximum order capacity of 50.

I am trying to communicate to the customer in their cart of the orders left before we close the ordering.

I need to get the total number of items already processing in each order + new orders in cart subtracted from the 50 maximum.

I have try it using this code:

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

How can I make this works as expected?

Thanks

Upvotes: 1

Views: 92

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 254363

To get your remaining order calculation from existing customer "processing" orders items and actual cart items, you can try this custom function (with an optional $user_id argument).

This is the code:

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

Code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.

This code is tested and functional.


Update for a "not user specific case":

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

This should work as you expect…

Upvotes: 1

Related Questions