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

本站所有文章均为博主人工写作,绝无AI辅助成分,请放心参阅。

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

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

图中倒数第三行是WooCommerce自带的邮箱设定,最后两行就是我添加的功能,指定优惠券对某些用户等级(role)的使用和禁用。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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列,便于运维识别

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 );

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 );

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

本站所有文章均为原创,欢迎转载,请注明文章出处:https://blog.brain1981.com/2709.html。百度和各类采集站皆不可信,搜索请谨慎鉴别。技术类文章一般都有时效性,本人习惯不定期对自己的博文进行修正和更新,因此请访问出处以查看本文的最新版本。

关注我们的微信公众号-JennyStudio 本站记录了近几年的工作中遇到的一些技术问题和解决过程,“作品集”还收录了本人的大部分作品展示。除了本博客外,我们的工作室网站 – JennyStudio,内有更多作品回顾和展示。
您也可以扫描左边的二维码,关注我们的微信公众号,在微信上查看我们的案例。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注