If you’re looking for a quick and simple way to limit WooCommerce Coupon usage to a specific user role, this tutorial is for you.
Using custom user roles in WordPress is a great way to classify customers on your WooCommerce website if you have a ton of them. By adding custom rolls, you can segment then into different groups, usually exporting them to mailing lists, and setting up specific rules for those individual groups. for example, you can create a custom user role and apply a custom coupon to that role.
The following code snippet will show you how to limit will coupon usage to a specific user role, without the need for a plugin or paid third party add on. This code was sourced from this incredible stackoverflow.com article, and we recommend reading through it to get some more context, information, and ideas.
// Add new field - usage restriction tab
function action_woocommerce_coupon_options_usage_restriction( $coupon_get_id, $coupon ) {
woocommerce_wp_text_input( array(
'id' => 'customer_user_role',
'label' => __( 'User role restrictions', 'woocommerce' ),
'placeholder' => __( 'No restrictions', 'woocommerce' ),
'description' => __( 'List of allowed user roles. Separate user roles with commas.', 'woocommerce' ),
'desc_tip' => true,
'type' => 'text',
));
}
add_action( 'woocommerce_coupon_options_usage_restriction', 'action_woocommerce_coupon_options_usage_restriction', 10, 2 );
// Save
function action_woocommerce_coupon_options_save( $post_id, $coupon ) {
update_post_meta( $post_id, 'customer_user_role', $_POST['customer_user_role'] );
}
add_action( 'woocommerce_coupon_options_save', 'action_woocommerce_coupon_options_save', 10, 2 );
// Valid
function filter_woocommerce_coupon_is_valid( $is_valid, $coupon, $discount ) {
// Get meta
$customer_user_role = $coupon->get_meta('customer_user_role');
// NOT empty
if( ! empty( $customer_user_role ) ) {
// Convert string to array
$customer_user_role = explode(', ', $customer_user_role);
// Get current user role
$user = wp_get_current_user();
$roles = ( array ) $user->roles;
// Compare
$compare = array_diff( $roles, $customer_user_role );
// NOT empty
if ( ! empty ( $compare ) ) {
$is_valid = false;
}
}
return $is_valid;
}
add_filter( 'woocommerce_coupon_is_valid', 'filter_woocommerce_coupon_is_valid', 10, 3 );
Code language: PHP (php)
Essentially, this code snippet adds an additional field in the usage restrictions section of a WooCommerce coupon page, allowing you to specify the roles that the coupon will be limited to. For example, we can create a new custom role called first responder. We can classify all our customers who are first responders into this role, and then offer them a 50% off coupon.
This coupon can be set to automatically apply to all of their purchases (additional code needed, check the StackOverflow entry for more info), or work normally, where they still need to enter it, but anybody without that user role attached to them will not be able to apply it.
We add this code with Advanced Scripts:
Then, all you need to do is create a coupon, and enter the user role that you want to restrict it to.
If you find yourself needing more features then just limiting a coupon usage to a specific user role, check out the Smart Coupons WooCommerce plugin. This is a premium add-on, and it brings with it a lot of custom rules that you can use, like:
This is almost what I'm looking for. I need to restrict the coupon by username, not user role. In other words I want to give a coupon to a single unique user. What parameter do I target(username, customer ID, email address etc..) and how do I edit this code?
Target the email address, that is what work best.
Hello I need help on my site everything works well for customers with a single role but in my configuration some can have several roles and in this case the coupon is refused.
I guess the original code checks if the role is allowed.
How to modify the code so that the check is no longer "the role is" but "the role contains"
Thank's