isotropic-2022
Share
Blog
Search
Menu

Restrict WooCommerce Coupons Based On User Roles

By James LePage
 on January 5, 2021
Last modified on January 7th, 2022

Restrict WooCommerce Coupons Based On User Roles

By James LePage
 on January 5, 2021
Last modified on January 7th, 2022

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.

Coupons

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:

  • automatic coupon issuance
  • advanced rules
  • store credits and gift cards
  • Shareable / Social Media / URL Coupons
Subscribe & Share
If you liked this content, subscribe for our monthly roundup of WordPress news, website inspiration, exclusive deals and interesting articles.
Unsubscribe at any time. We do not spam and will never sell or share your email.
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
David
David
6 months ago

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?

Kwadwo
5 months ago
Reply to  David

Target the email address, that is what work best.

Article By
James LePage
Contributors/Editors
notloggedin
James LePage is the founder of Isotropic Design, a digital agency that builds WordPress websites. I read, write and design constantly!
We're looking for new authors. Explore Isotropic Jobs.
Close
🇺🇸 English
Save posts, access exclusive content, join members-only communities and more.
Dark Mode Toggle
Love web dev? Join the Isotropic weekly newsletter.
cross
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram
0

Your Cart is Empty