Restrict WooCommerce Coupons Based On User Roles

Published: 7 months ago

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 );

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
Join Our Newsletter
Here's what you get: A once a week newsletter that contains original content, aggregated posts, WordPress deals, and anything else we think you'd like. We don't spam or sell your email address. Unsub at any time.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
IsoGroup- Web Dev/Design, WordPress and More

Affiliate Disclosure: Some links on this website are affiliate links. We make commissions for purchases made through those links.

We only recommend items/tools that we've personally used and like.

Full Disclosure | FTC Statement

Join Our Newsletter
Over 4,000 Design Lovers and Problem Solvers.

Author

A relationship driven website creation company.

Connect

© ISOTROPIC, LLC 2016-2021
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram