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

I am using the woocommerce plugin in the theme. I need functionality where I need to check the total amount of orders made by a client. Based on this, I offer them a discount in coupons. Part of the coupons is understandable. But I can’t get the total amount of the entire order made by the user.

what's my plan: if the user’s purchase exceeds $ 300, we will give him a coupon. I use this action for this. But with difficulties with the database query for the amount of orders by the 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 also trying to get an order by the user ID that I received from my-orders.php in the woocommerce / myaccount folder. I am trying to run this code in function.php, but returning an 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;

- , function.php. , . , .

+3
2

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

, wp , , woocommerce_payment_complete,

,

+2

5 , , , , .

global $wpdb;
$top_customer_query = "";
$top_customer_query .= "SELECT SUM(wc_postmeta1.meta_value) AS 'Total'
,wc_postmeta2.meta_value AS 'BillingEmail'
,CONCAT(wc_postmeta3.meta_value, ' ',wc_postmeta5.meta_value) AS 
 BillingName
,Count(wc_postmeta2.meta_value) AS 'OrderCount' FROM ";
 $top_customer_query .= "{$wpdb->prefix}posts as wc_posts
LEFT JOIN  {$wpdb->prefix}postmeta as wc_postmeta1 ON 
wc_postmeta1.post_id=wc_posts.ID
LEFT JOIN  {$wpdb->prefix}postmeta as wc_postmeta2 ON 
wc_postmeta2.post_id=wc_posts.ID
LEFT JOIN  {$wpdb->prefix}postmeta as wc_postmeta3 ON 
wc_postmeta3.post_id=wc_posts.ID
LEFT JOIN  {$wpdb->prefix}postmeta as wc_postmeta5 ON 
wc_postmeta5.post_id=wc_posts.ID";
$top_customer_query .= " WHERE wc_posts.post_type='shop_order'
AND wc_postmeta1.meta_key='_order_total' 
AND wc_postmeta2.meta_key='_billing_email'  
AND wc_postmeta3.meta_key='_billing_first_name'
AND wc_postmeta5.meta_key='_billing_last_name'";
$top_customer_query .= " AND  wc_posts.post_status IN ('wc-completed')";
$top_customer_query .= " GROUP BY  wc_postmeta2.meta_value";
$top_customer_query .= " Order By Total DESC";
$top_customer_query .= " LIMIT 0,5";
$top_5_customers = $wpdb->get_results($top_customer_query, OBJECT);
foreach ($top_5_customers as $customer) {
?>
<tr>
<td>
    <?php echo $customer->BillingName; ?>

0

Source: https://habr.com/ru/post/1626960/


All Articles