Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get total sum of orders by a user in woocommerce?

I am using woocommerce plugin in a theme. I want a functionality where I need to check the total sum of orders made by a customer. On the basis of that i will offer them discount in coupons. Coupons part is clear. But I am not able to get total sum of all order made by a user.

what my plan is : if a users purchase will exceeds the $300 , we will provide him coupon. for this I am using this action. But having difficulty with query form database for sum of orders by user.

function so_27969258_track_orders_per_customer($order_id){


    $order = new WC_Order( $order_id );
    $myuser_id = (int)$order->user_id;
    $user_info = get_userdata($myuser_id);
    $items = $order->get_items();
    foreach ($items as $item) {

    }
    return $order_id;

}
add_action( 'woocommerce_payment_complete', 'so_27969258_track_orders_per_customer' );

I am aslo trying to get the order by a user id which I got from the my-orders.php in woocommerce/myaccount folder. I am trying to run this code in function.php but returning the empty array.

$customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
    'numberposts' => $order_count,
    'meta_key'    => '_customer_user',
    'meta_value'  => get_current_user_id(),
    'post_type'   => wc_get_order_types( 'view-orders' ),
    'post_status' => array_keys( wc_get_order_statuses() )
) ) );


var_dump($customer_orders);
if ( $customer_orders ) : 


    foreach ( $customer_orders as $customer_order ) {
                $order = wc_get_order( $customer_order );
                $order->populate( $customer_order );
                $item_count = $order->get_item_count();

                 echo $order->get_order_number(); 
                 echo wc_get_order_status_name( $order->get_status() );
                 echo sprintf( _n( '%s for %s item', '%s for %s items', $item_count, 'woocommerce' ), $order->get_formatted_order_total(), $item_count );

            }



 endif;

Can anybody help me how this code will work in function.php .I know am missing lots of things. Please suggest.

like image 667
Vijay Lal Avatar asked Feb 09 '23 18:02

Vijay Lal


2 Answers

You can use the following function to get total sum of completed orders of a user

public function get_customer_total_order() {
    $customer_orders = get_posts( array(
        'numberposts' => - 1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => array( 'shop_order' ),
        'post_status' => array( 'wc-completed' )
    ) );

    $total = 0;
    foreach ( $customer_orders as $customer_order ) {
        $order = wc_get_order( $customer_order );
        $total += $order->get_total();
    }

    return $total;
}

But this function must be called when wp loaded completely, so if you use this function on your action woocommerce_payment_complete, it will work

Hope it helps

like image 98
Mehran Avatar answered Feb 16 '23 00:02

Mehran


The WC API has a method to get orders, where you can add various $args. Here is a function I am using:

function get_user_orders_total($user_id) {
    // Use other args to filter more
    $args = array(
        'customer_id' => $user_id,
        'limit' => -1, // to get _all_ orders from this user
    );
    // call WC API
    $orders = wc_get_orders($args);

    if (empty($orders) || !is_array($orders)) {
        return false;
    }

    // One implementation of how to sum up all the totals
    $total = array_reduce($orders, function ($carry, $order) {
        $carry += (float)$order->get_total();

        return $carry;
    }, 0.0);

    return $total;
}
like image 26
kontur Avatar answered Feb 16 '23 00:02

kontur