当前位置:首页文章笔记建站教程WooCommerce无插件增加优惠券功能,限定/禁止给指定用户角色(role)使用

WooCommerce无插件增加优惠券功能,限定/禁止给指定用户角色(role)使用

WooCommerce 提供了基本的优惠券 Coupon 功能,优惠券可以限定/禁止使用的产品、产品分类、限定最小和最大金额等,这些功能很实用,但仍然比较单薄,尤其是对指定用户的限制很弱,只能限定给指定邮箱的用户使用。邮箱可以使用通配符比如*@google.com,这可能比较符合国外网站的运营习惯,但对我们来说不太直观好用。我们通常希望实现的是给指定用户等级设定优惠券的使用或者禁用。本文记录如何开发出这个功能。
WooCommerce无插件增加优惠券功能,限定/禁止给指定用户角色(role)使用
图中倒数第三行是 WooCommerce 自带的邮箱设定,最后两行就是我添加的功能,指定优惠券对某些用户等级(role)的使用和禁用。

先上代码来添加这两个后台选项:

  1. function brain1981_woocommerce_coupon_options_usage_for_role( $coupon_get_id, $coupon ) {
  2. //两个字段都可以指定用户角色
  3. woocommerce_wp_select(
  4. array(
  5. ‘id’ => ‘customer_user_role’,
  6. ‘label’ => __( ‘User role limitations’, ‘woocommerce’ ),
  7. ‘options’ => array(
  8. ‘0’ => __( ‘Please Select’, ‘woocommerce’ ),
  9. ‘subscriber’ => __( ‘Subscriber’, ‘woocommerce’ ),
  10. ‘customer’ => __( ‘Customer’, ‘woocommerce’ ),
  11. ‘author’ => __( ‘Author’, ‘woocommerce’ ),
  12. ‘editor’ => __( ‘Editor’, ‘woocommerce’ )
  13. )
  14. )
  15. );
  16. woocommerce_wp_select(
  17. array(
  18. ‘id’ => ‘customer_user_role_restriction’,
  19. ‘label’ => __( ‘User role restriction’, ‘woocommerce’ ),
  20. ‘options’ => array(
  21. ‘0’ => __( ‘Please Select’, ‘woocommerce’ ),
  22. ‘subscriber’ => __( ‘Subscriber’, ‘woocommerce’ ),
  23. ‘customer’ => __( ‘Customer’, ‘woocommerce’ ),
  24. ‘author’ => __( ‘Author’, ‘woocommerce’ ),
  25. ‘editor’ => __( ‘Editor’, ‘woocommerce’ )
  26. )
  27. )
  28. );
  29. }
  30. add_action( ‘woocommerce_coupon_options_usage_restriction’, ‘brain1981_woocommerce_coupon_options_usage_for_role’, 10, 2 );
  31. //保存
  32. function brain1981_woocommerce_coupon_meta_save( $post_id, $coupon ) {
  33. update_post_meta( $post_id, ‘customer_user_role’, $_POST[‘customer_user_role’] );
  34. update_post_meta( $post_id, ‘customer_user_role_restriction’, $_POST[‘customer_user_role’] );
  35. }
  36. add_action( ‘woocommerce_coupon_options_save’, ‘brain1981_woocommerce_coupon_meta_save’, 10, 2 );

然后是后台的优惠券列表里增加 2 列,便于运维识别

  1. function brain1981_edit_shop_coupon_columns( $columns ) {
  2. $columns[‘coupon_role’] = __( ‘Only for Role’, ‘woocommerce’ );
  3. $columns[‘coupon_role_restriction’] = __( ‘Restriction for Role’, ‘woocommerce’ );
  4. return $columns;
  5. }
  6. add_filter( ‘manage_edit-shop_coupon_columns’, ‘brain1981_edit_shop_coupon_columns’, 10, 1 );
  7. function brain1981_shop_coupon_posts_custom_column( $column, $post_id ) {
  8. if ( $column == ‘coupon_role’ ) {
  9. $coupon_role = get_post_meta( $post_id, ‘customer_user_role’, true);
  10. if ( !empty( $coupon_role ) ) {
  11. echo $coupon_role;
  12. }
  13. }
  14. if ( $column == ‘coupon_role_restriction’ ) {
  15. $coupon_role_restriction = get_post_meta( $post_id, ‘customer_user_role_restriction’, true);
  16. if ( !empty( $coupon_role_restriction ) ) {
  17. echo $coupon_role_restriction ;
  18. }
  19. }
  20. }
  21. add_action( ‘manage_shop_coupon_posts_custom_column’ , ‘brain1981_shop_coupon_posts_custom_column’, 10, 2 );

最后,用户在使用这个优惠券的时候,需要增加对用户等级的验证:

  1. function brain1981_woocommerce_coupon_is_valid( $is_valid, $coupon, $discount ) {
  2. // Get meta
  3. $customer_user_role = $coupon->get_meta(‘customer_user_role’);
  4. $customer_user_role_restriction = $coupon->get_meta(‘customer_user_role_restriction’);
  5. //限制只让这个等级使用
  6. if( !empty( $customer_user_role ) ) {
  7. wc_current_user_has_role($customer_user_role)? $is_valid = true : $is_valid = false ;
  8. }
  9. //限制不让这个等级使用
  10. if( !empty( $customer_user_role_restriction ) ) {
  11. if (wc_current_user_has_role($customer_user_role_restriction ) ) $is_valid = false ;
  12. }
  13. return $is_valid;
  14. }
  15. add_filter( ‘woocommerce_coupon_is_valid’, ‘brain1981_woocommerce_coupon_is_valid’, 10, 3 );

如此,即完成了一个简单版的优惠券功能拓展开发,不需要添加任何插件

温馨提示:

文章标题:WooCommerce无插件增加优惠券功能,限定/禁止给指定用户角色(role)使用

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

更新时间:2022年04月08日

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

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

作为WordPress建站的开发者为什么坚持尽可能少用插件

2022-4-8 17:02:57

建站教程

WooCommerce报致命错误 Fatal error:call to undefined function putenv()

2022-4-12 22:59:40

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