当前位置:首页文章笔记建站教程WooCommerce 获取用户总消费金额

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 获取用户总消费金额

文章链接:https://www.wuyanshuo.cn/710.html

更新时间:2022年04月13日

本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:service@wuyanshuo.cn我们将第一时间处理! 资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持。 所有资源仅限于参考和学习,版权归原作者所有,更多请阅读无言说网络服务协议

给TA打赏
共{{data.count}}人
人已打赏
建站教程

WordPress 在安装时为什么会记录域名

2022-4-12 23:03:49

建站教程

后台设置页面使用WordPress媒体上传工具上传图片

2022-4-13 4:58:45

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索
'