Home > OS >  Show custom text based on total spent by user in WooCommerce
Show custom text based on total spent by user in WooCommerce

Time:12-07

So I want to run a Tier program on WooCommerce and I need to show the user the Tier level based on total spent on website.

I'm using How to get the total amount spent by a user (customer) in WooCommerce? answer code

I have 4 tier levels so I would like to show if user spent more than 600$ some text like "congrats you are now tier 1" and so on.

I appreciate your help

CodePudding user response:

You can add the if condition. try the below code.

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;
    }
}
add_shortcode('user_total_spent', 'get_user_total_spent');
  • Related