WordPress教程

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

阿里云

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

图中倒数第三行是 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. //保存
  33. function brain1981_woocommerce_coupon_meta_save( $post_id, $coupon ) {
  34. 	update_post_meta( $post_id, 'customer_user_role', $_POST['customer_user_role'] );
  35. 	update_post_meta( $post_id, 'customer_user_role_restriction', $_POST['customer_user_role'] );
  36. }
  37. 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)使用

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

收藏
(0)

发表回复

热销模板

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

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