WooCommerce 提供了基本的优惠券 Coupon 功能,优惠券可以限定/禁止使用的产品、产品分类、限定最小和最大金额等,这些功能很实用,但仍然比较单薄,尤其是对指定用户的限制很弱,只能限定给指定邮箱的用户使用。邮箱可以使用通配符比如*@google.com,这可能比较符合国外网站的运营习惯,但对我们来说不太直观好用。我们通常希望实现的是给指定用户等级设定优惠券的使用或者禁用。本文记录如何开发出这个功能。
图中倒数第三行是 WooCommerce 自带的邮箱设定,最后两行就是我添加的功能,指定优惠券对某些用户等级(role)的使用和禁用。
先上代码来添加这两个后台选项:
- function brain1981_woocommerce_coupon_options_usage_for_role( $coupon_get_id, $coupon ) {
- //两个字段都可以指定用户角色
- woocommerce_wp_select(
- array(
- 'id' => 'customer_user_role',
- 'label' => __( 'User role limitations', 'woocommerce' ),
- 'options' => array(
- '0' => __( 'Please Select', 'woocommerce' ),
- 'subscriber' => __( 'Subscriber', 'woocommerce' ),
- 'customer' => __( 'Customer', 'woocommerce' ),
- 'author' => __( 'Author', 'woocommerce' ),
- 'editor' => __( 'Editor', 'woocommerce' )
- )
- )
- );
- woocommerce_wp_select(
- array(
- 'id' => 'customer_user_role_restriction',
- 'label' => __( 'User role restriction', 'woocommerce' ),
- 'options' => array(
- '0' => __( 'Please Select', 'woocommerce' ),
- 'subscriber' => __( 'Subscriber', 'woocommerce' ),
- 'customer' => __( 'Customer', 'woocommerce' ),
- 'author' => __( 'Author', 'woocommerce' ),
- 'editor' => __( 'Editor', 'woocommerce' )
- )
- )
- );
- }
- add_action( 'woocommerce_coupon_options_usage_restriction', 'brain1981_woocommerce_coupon_options_usage_for_role', 10, 2 );
- //保存
- function brain1981_woocommerce_coupon_meta_save( $post_id, $coupon ) {
- update_post_meta( $post_id, 'customer_user_role', $_POST['customer_user_role'] );
- update_post_meta( $post_id, 'customer_user_role_restriction', $_POST['customer_user_role'] );
- }
- add_action( 'woocommerce_coupon_options_save', 'brain1981_woocommerce_coupon_meta_save', 10, 2 );
然后是后台的优惠券列表里增加 2 列,便于运维识别
- function brain1981_edit_shop_coupon_columns( $columns ) {
- $columns['coupon_role'] = __( 'Only for Role', 'woocommerce' );
- $columns['coupon_role_restriction'] = __( 'Restriction for Role', 'woocommerce' );
- return $columns;
- }
- add_filter( 'manage_edit-shop_coupon_columns', 'brain1981_edit_shop_coupon_columns', 10, 1 );
- function brain1981_shop_coupon_posts_custom_column( $column, $post_id ) {
- if ( $column == 'coupon_role' ) {
- $coupon_role = get_post_meta( $post_id, 'customer_user_role', true);
- if ( !empty( $coupon_role ) ) {
- echo $coupon_role;
- }
- }
- if ( $column == 'coupon_role_restriction' ) {
- $coupon_role_restriction = get_post_meta( $post_id, 'customer_user_role_restriction', true);
- if ( !empty( $coupon_role_restriction ) ) {
- echo $coupon_role_restriction ;
- }
- }
- }
- add_action( 'manage_shop_coupon_posts_custom_column' , 'brain1981_shop_coupon_posts_custom_column', 10, 2 );
最后,用户在使用这个优惠券的时候,需要增加对用户等级的验证:
- function brain1981_woocommerce_coupon_is_valid( $is_valid, $coupon, $discount ) {
- // Get meta
- $customer_user_role = $coupon->get_meta('customer_user_role');
- $customer_user_role_restriction = $coupon->get_meta('customer_user_role_restriction');
- //限制只让这个等级使用
- if( !empty( $customer_user_role ) ) {
- wc_current_user_has_role($customer_user_role)? $is_valid = true : $is_valid = false ;
- }
- //限制不让这个等级使用
- if( !empty( $customer_user_role_restriction ) ) {
- if (wc_current_user_has_role($customer_user_role_restriction ) ) $is_valid = false ;
- }
- return $is_valid;
- }
- add_filter( 'woocommerce_coupon_is_valid', 'brain1981_woocommerce_coupon_is_valid', 10, 3 );
如此,即完成了一个简单版的优惠券功能拓展开发,不需要添加任何插件。
- 1、本站大部分内容均收集于网络,若内容侵犯到您的权益,请发送邮件至:service@wuyanshuo.cn,我们将第一时间处理!
- 2、本站资源所需价格并非资源售卖价格,是收集、整理、编辑以及本站运营的适当补贴,并且本站不提供任何免费技术服务。
- 3、所有资源仅限参考和学习,版权归原作者所有,更多请阅读无言说网络服务器协议。