Search code examples
phpwordpresswoocommerceshortcodeorders

Get the customer's total spend for a period of 1 year in WooCommerce


Using the code from Show custom text based on total spent by user in WooCommerce:

add_shortcode('user_total_spent', 'get_user_total_spent');
function get_user_total_spent( $atts ) {

    extract( shortcode_atts( array(
        'user_id' => get_current_user_id(),
    ), $atts, 'user_total_spent' ) );

    if( $user_id > 0 ) {
        $customer = new WC_Customer( $user_id ); // Get WC_Customer Object

        $total_spent = $customer->get_total_spent(); // Get total spent amount

        if( $total_spent > 600 && $total_spent < 1200 ){
            $text = __( 'congrats you are now tier 1', 'woocommerce' );
        }elseif( $total_spent > 1200 ){
            $text = __( 'congrats you are now tier 2', 'woocommerce' );
        }else{
            $text = __( 'congrats you are now tier 3', 'woocommerce' );
        }

        $total_spent = wc_price( $total_spent );

        return "Total Amount Spent: ".$total_spent." ".$text;
    }
}

This code receives the customer's total spend.

Is there a way to receive the total spend only for the past year, from customer “Completed” orders?

For example, if today is 2024/03/24, then the total spend should be taken only for the sum of “Completed” orders for the period from 2023/03/24 to 2024/03/24.


Solution

  • You can use the following custom utility function to get the total spent from "completed" customer for a specific period (from the past year):

    // Utility function: Get customer orders total amount from specific status on a period
    function get_orders_sum_amount_for( $user_id, $period = '1 year', $status = 'completed' ) {
        global $wpdb;
        return $wpdb->get_var( $wpdb->prepare( "
            SELECT SUM(total_amount)
            FROM {$wpdb->prefix}wc_orders
            WHERE status = %s
            AND customer_id = %d
            AND date_created_gmt >= %s
        ", 'wc-'.$status, $user_id, date('Y-m-d H:i:s', strtotime('-'.$period) ) ) );
    }
    

    Then you can use it in the shortcode function like:

    add_shortcode('user_year_total_spent', 'get_user_year_total_spent');
    function get_user_year_total_spent( $atts ) {
        extract( shortcode_atts( array(
            'user_id' => get_current_user_id(),
        ), $atts, 'user_year_total_spent' ) );
    
        if( ! $user_id ) return;
    
        $total_spent = get_orders_sum_amount_for( $user_id ); // Get total spent amount
    
        if( $total_spent >= 1200 ) {
            $text = __('Congrats you are now tier 3.', 'woocommerce');
        } elseif ( $total_spent >= 600 ) {
            $text = __('Congrats you are now tier 2.', 'woocommerce');
        } else {
            $text = __('Congrats you are now tier 1.', 'woocommerce');
        }
        return sprintf(__('Total Amount Spent: %s. %s', 'woocommerce'), wc_price($total_spent), $text);
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and works.

    SHORTCODE USAGE:

    • In WP post/page editor, or in some widgets: [user_year_total_spent]
    • In PHP code: echo do_shortcode('[user_year_total_spent]');