WooCommerce gets the total amount spent by the user

Upgrading a user to a certain level of membership based on the total amount spent by the user to enjoy some benefits is a common practice in membership marketing. In WooCommerce, we can use wc_get_customer_total_spent function to easily get the total amount of user spending.

We need to pass in the user ID as the only parameter to this function, and then we'll get the string of the total amount spent by the user returned by the function.

How does this function work?

The source code for this function is in thewoocommerceincludeswc-user-functions.php file, the code details are as follows:

/**
 * Get total spent by customer.
 * @param int $user_id User ID.
 * @param int $user_id User ID.
 * @return string
 */@return string
function wc_get_customer_total_spent( $user_id ) {
$customer = new WC_Customer( $user_id ); function wc_get_customer_total_spent( $user_id ) { $customer
return $customer->get_total_spent();
}

The get_total_spent method in the above code is a method in the WooCommerce client class. In this method, the program first determines whether there is a _money_spent field in the user-defined field, if there is a direct return, if not, query the database to get the total amount spent by the user, and then save it to the user-defined field. From the program time-consuming , get the user-defined field than directly query the user's consumption record to get the total amount of consumption will be much less , this is a caching method , we can refer to in the development of themes and plug-ins .

/**
 * Return how much money this customer has spent.
 *
 * @since 3.0.0
 * @param WC_Customer $customer Customer object.
 * @return float
 */
public function get_total_spent( &$customer ) {
	$spent = apply_filters(
		'woocommerce_customer_get_total_spent',
		get_user_meta( $customer->get_id(), '_money_spent', true ),
		$customer
	);
	if ( '' === $spent ) {
		global $wpdb;
		$statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
		$spent    = $wpdb->get_var(
			// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
			apply_filters(
				'woocommerce_customer_get_total_spent_query',
				"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     = '" . esc_sql( $customer->get_id() ) . "'
				AND     posts.post_type     = 'shop_order'
				AND     posts.post_status   IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
				AND     meta2.meta_key      = '_order_total'",
				$customer
			)
			// phpcs:enable
		);
		if ( ! $spent ) {
			$spent = 0;
		}
		update_user_meta( $customer->get_id(), '_money_spent', $spent );
	}
	return wc_format_decimal( $spent, 2 );
}

Which statuses of orders are counted towards the user's total consumption

According to the default settings of WooCommerce, only orders that are in process or completed will be counted towards the user's total spend. If you need to change the status of the orders that are counted towards the total spend, we can use thewoocommerce_order_is_paid_statuses Filter is modified.

What you can do with this function

For the need for membership marketing website, we can use this function to get the total user consumption, and then if the consumption of more than a certain amount, both the user can be considered a VIP user, you can allow him to enjoy some of the rights and interests of ordinary users can not enjoy, such as the purchase of only VIP users can buy goods.

In the sample code below, if the user's total spending exceeds 500, we display a message on the user's account page reminding the user that he is our VIP member and can purchase VIP limited items.

add_action('woocommerce_account_dashboard', function ()
{
    $user_id = get_current_user_id();
    if (wc_get_customer_total_spent($user_id) > 500) {
        echo '<div class="woocommerce-message"><a class="button" href="/en/shop/vip/">Check out the VIP limited items</a>Congratulations, you are our valued VIP member.</div>';
    }
});

Getting the total amount of user spending can be an expensive operation at the database level. For sites with high traffic, we can reduce this operation by adjusting user roles based on user spending, and then set up a timed task to handle upgrading a user's VIP role every day when the site is idle.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *