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.
You can use the following custom utility function to get the total spent from "completed" customer for a specific period (from the past year):
Then you can use it in the shortcode function like:
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
SHORTCODE USAGE:
[user_year_total_spent]echo do_shortcode('[user_year_total_spent]');