• Hello. I have been working on an issue for a little while and can’t seem to find the solution. I am attempting to develop a function that changes a customer’s account type (ie from ‘customer’ to something else) once their account spend hits a certain amount.

    Here is what I have tried so far. I very new to writing php and functions so I apologize upfront for the mess this may be. Any help would be appreciated.

    /**
     * Get total spent by customer
     * @param  int $user_id
     * @return string
     */
    function wc_get_customer_total_spent( $user_id ) {
    if ( ! $spent = get_user_meta( $user_id, '_money_spent' < 50 ) ) {
    global $wpdb;
     // Remove role
            $user->remove_role( 'customer' ); 
    
            // Add role
            $user->add_role( 'procon' );
    
    $spent = $wpdb->get_var( "SELECT SUM(meta2.meta_value)
    FROM $wpdb->posts as posts
    
    LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
    LEFT JOIN {$wpdb->postmeta} AS meta2 ON posts.ID = meta2.post_id
    
    WHERE   meta.meta_key       = '_customer_user'
    AND     meta.meta_value     = $user_id
    AND     posts.post_type     IN ('" . implode( "','", wc_get_order_types( 'reports' ) ) . "')
    AND     posts.post_status   IN ( 'wc-completed', 'wc-processing' )
    AND     meta2.meta_key      = '_order_total'
    " 
    
            );
    
    update_user_meta( $user_id, '_money_spent', $spent );
    }
    
    return $spent;
    }
Viewing 9 replies - 1 through 9 (of 9 total)
  • Ok, so I have not tested it, but it could do the trick.

    The first function finds the amount spent on each order from a specific email address. Usage: woocommerce_get_amount_spent('[email protected]')

    The second function changes the user role if the user has spent a certain amount of cash. Usage: customer_change_role(1, 400, 'iuseallmymoneyhere') The first parameter is the user ID, the second is the minimum amount to achieve the role and the third is the name of the role.

    You could hook this in to the checkout process of WooCommerce so that immediately after they complete an order they get the new role.

    add_filter('woocommerce_payment_complete', function($order_id) {
    
    	$order = new WC_Order($order);
    	$user_id = $order->user_id;
    
    	if($user_id) {
    		customer_change_role($user_id, 600, 'bigspender');
    	}
    
    });
    /**
     * Gets the amount an email has spent
     * @param  string $email Users email
     * @return float
     */
    function woocommerce_get_amount_spent($email) {
    
    	$orders = get_posts( array(
    	    'meta_key'    		=> '_billing_email',
    	    'meta_value'  		=> $email,
    	    'post_type'   		=> 'shop_order',
    	    'posts_per_page'	=>-1
    	) );
    
    	$spent = 0;
    	foreach($orders as $shop_order) {
    		$order = new WC_Order($shop_order->ID);
    		$spent += $order->get_total();
    	}
    
    	return $spent;
    
    }
    
    /**
     * Change a customers role when they spend big money
     * @param  integer 	$user_id   	The ID of the user. Current user when none supplied
     * @param  integer 	$threshold 	How much money do we need?
     * @param  string 	$role 		Which role to give
     * @return void
     */
    function customer_change_role($user_id = null, $threshold = 600, $role = 'bigspender') {
    
    	// when the user_id was not supplied we'll try and figure it out
    	if($user_id === null) {
    		$user_id = get_current_user_id();
    	}
    
    	// this probably means the user is not logged in
    	if( !$user_id ) {
    		return;
    	}
    
    	// get the user object
    	$user = get_user_by('id', $user_id);
    
    	// nope, something went wrong
    	if(!$user) {
    		return;
    	}
    
    	// the user is a big spender, lets give him a role
    	if(woocommerce_get_amount_spent($user->data->user_email) >= $threshold) {
    		$user->add_role($role);
    	}
    	// this mofo ain't using money. take it back!
    	else {
    		$user->remove_role($role);
    	}
    
    }
    Thread Starter chriswindsor

    (@chriswindsor)

    Thanks for the response. Would this be pulling individual order amounts or pull the users entire lifetime account spend? For example, if the threshold was set to $10,000 and the customer’s account was at $9,000. Then if they spent over $100, their account type would them change.

    Appreciate it.

    This would pull the entire lifetime account spend. So if the threshold is set to 10000, an order of 100 would not trigger an account type change if the current account was at 9000. But an order or 1000 would.

    Thread Starter chriswindsor

    (@chriswindsor)

    I’m sorry, missed a zero. Thanks a bunch. I will test it out and let you know if it does the trick

    Thread Starter chriswindsor

    (@chriswindsor)

    So I put the code in my function.php file and it does not change the user role.

    Any suggestions? This is what it looks like now with the threshold changes and the user role name change.

    /*Auto Account Switch*/
    add_filter('woocommerce_payment_complete', function($order_id) {
    
    	$order = new WC_Order($order);
    	$user_id = $order->user_id;
    
    	if($user_id) {
    		customer_change_role($user_id, 600, 'procon');
    	}
    
    });
    
    /**
     * Gets the amount an email has spent
     * @param  string $email Users email
     * @return float
     */
    function woocommerce_get_amount_spent($email) {
    
    	$orders = get_posts( array(
    	    'meta_key'    		=> '_billing_email',
    	    'meta_value'  		=> $email,
    	    'post_type'   		=> 'shop_order',
    	    'posts_per_page'	=>-1
    	) );
    
    	$spent = 0;
    	foreach($orders as $shop_order) {
    		$order = new WC_Order($shop_order->ID);
    		$spent += $order->get_total();
    	}
    
    	return $spent;
    
    }
    
    /**
     * Change a customers role when they spend big money
     * @param  integer 	$user_id   	The ID of the user. Current user when none supplied
     * @param  integer 	$threshold 	How much money do we need?
     * @param  string 	$role 		Which role to give
     * @return void
     */
    
    function customer_change_role($user_id = null, $threshold = 600, $role = 'procon') {
    
    	// when the user_id was not supplied we'll try and figure it out
    	if($user_id === null) {
    		$user_id = get_current_user_id();
    	}
    
    	// this probably means the user is not logged in
    	if( !$user_id ) {
    		return;
    	}
    
    	// get the user object
    	$user = get_user_by('id', $user_id);
    
    	// nope, something went wrong
    	if(!$user) {
    		return;
    	}
    
    	// the user is a big spender, lets give him a role
    	if(woocommerce_get_amount_spent($user->data->user_email) >= $threshold) {
    		$user->add_role($role);
    	}
    	// this mofo ain't using money. take it back!
    	else {
    		$user->remove_role($role);
    	}
    }
    Thread Starter chriswindsor

    (@chriswindsor)

    Any thoughts?

    The woocommerce_payment_complete action is run once a payment has been completed. Have you tested that?

    Thread Starter chriswindsor

    (@chriswindsor)

    To be honest, I am not sure how to test that. I have tried placing that action in the checkout process and it did not seem to work.

    Which file should this be in?

    This should be placed in your functions.php file. You can check the WooCommerce hook reference to check if there is a more appropriate action you could use if your checkout does not involve payments.

    https://docs.woothemes.com/wc-apidocs/hook-docs.html

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Change user role based on account spend woocommerce’ is closed to new replies.