WordPress教程

WooCommerce 获取用户总消费金额

阿里云

根据用户总消费金额升级用户为某个级别的会员,享受一些优惠是会员营销中常见的套路。在 WooCommerce 中,我们可以用 wc_get_customer_total_spent 函数轻松获取用户总消费金额。

我们需要传入用户 ID 作为该函数的唯一一个参数,然后我们会得到该函数返回的用户总消费金额字符串。

也想出现在这里?联系我们
创客主机

该函数的源码在 woocommerce\includes\wc-user-functions.php 文件中,代码详情如下:

  1. /**
  2.  * Get total spent by customer.
  3.  *
  4.  * @param  int $user_id User ID.
  5.  * @return string
  6.  */
  7. function wc_get_customer_total_spent( $user_id ) {
  8. 	$customer = new WC_Customer( $user_id );
  9. 	return $customer->get_total_spent();
  10. }

上面代码中的 get_total_spent 方法是 WooCommerce 客户类中的一个方法,在该方法中,程序先判断是否有 _money_spent 字段在用户自定义字段中,如果有直接返回,如果没有,查询数据库获取用户总消费,再保存到用户自定义字段中。从程序耗时来说,获取用户自定义字段比直接查询用户消费记录得到总消费金额会少很多,这是一种缓存方法,我们在开发主题插件的时候可以参考。

  1. /**
  2.  * Return how much money this customer has spent.
  3.  *
  4.  * @since 3.0.0
  5.  * @param WC_Customer $customer Customer object.
  6.  * @return float
  7.  */
  8. public function get_total_spent( &$customer ) {
  9. 	$spent = apply_filters(
  10. 		'woocommerce_customer_get_total_spent',
  11. 		get_user_meta( $customer->get_id(), '_money_spent', true ),
  12. 		$customer
  13. 	);
  14. 	if ( '' === $spent ) {
  15. 		global $wpdb;
  16. 		$statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
  17. 		$spent    = $wpdb->get_var(
  18. 			// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
  19. 			apply_filters(
  20. 				'woocommerce_customer_get_total_spent_query',
  21. 				"SELECT SUM(meta2.meta_value)
  22. 				FROM $wpdb->posts as posts
  23. 				LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
  24. 				LEFT JOIN {$wpdb->postmeta} AS meta2 ON posts.ID = meta2.post_id
  25. 				WHERE   meta.meta_key       = '_customer_user'
  26. 				AND     meta.meta_value     = '" . esc_sql( $customer->get_id() ) . "'
  27. 				AND     posts.post_type     = 'shop_order'
  28. 				AND     posts.post_status   IN ( 'wc-" . implode( "','wc-", $statuses ) . "' )
  29. 				AND     meta2.meta_key      = '_order_total'",
  30. 				$customer
  31. 			)
  32. 			// phpcs:enable
  33. 		);
  34. 		if ( ! $spent ) {
  35. 			$spent = 0;
  36. 		}
  37. 		update_user_meta( $customer->get_id(), '_money_spent', $spent );
  38. 	}
  39. 	return wc_format_decimal( $spent, 2 );
  40. }

根据 WooCommerce 默认设置,只有处理中或已完成的订单才会计入用户总消费。如果需要修改计入总消费的订单状态,我们可以使用 woocommerce_order_is_paid_statuses Filter 进行修改。
使用这个函数可以干些什么

对于需要进行会员营销 网站,我们可以通过这个函数获取用户总消费,然后如果消费超过了某个金额,既可以认为该用户 VIP 用户,可以允许他享受一些普通用户享受不到权益,比如购买只有 VIP 用户才能购买 商品。

下面的示例代码中,如果用户总消费超过了 500,我们在用户的账户页面显示一条消息,提醒用户他是我们的 VIP 会员,可以购买 VIP 限定的商品。

  1. add_action('woocommerce_account_dashboard', function ()
  2. {
  3.     $user_id = get_current_user_id();
  4.     if (wc_get_customer_total_spent($user_id) > 500) {
  5.         echo '<div class="woocommerce-message"><a class="button" href="/shop/vip">查看VIP限定商品</a>恭喜,您是我们尊贵的VIP会员。</div>';
  6.     }
  7. });

获取用户总消费金额在数据库层面可能是一个比较昂贵的操作,对于流量比较大的网站,我们可以根据用户的消费调整用户角色来减少这个操作,然后设置定时任务,每天在网站闲时处理升级用户 VIP 角色的任务。

WooCommerce 获取用户总消费金额

已有 188 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!