Skip to main content

WooCommerce coupon for logged in user only

Use WooCommerce hook woocommerce_coupon_is_valid to check if coupon is valid.

Example shows how to make sure certain coupons are valid for logged in users only.
Same example can be use to extend validation, for example using global $current_user would allow you to validate any user information, eg: restrict coupon based on user role.


  1. Great code! Thanks you so much.

    One question:

    how I print a custom notices when return false instead default error message? I trying this but notices is added not changed.

    wc_add_notice( __( ‘Coupon only for logged users.’, ‘woocommerce’ ), ‘error’ );

    Thanks again.

    1. Hi JR, to be honest not sure.
      You can try with a filter instead woocommerce_add_error.
      That would look something like:

      add_filter('woocommerce_add_error', function ($message) {
      if ( 'default-returned-text' == $message ) {
      return 'My custom message';
      return $message;
      }, 10 );

      Or maybe try with woocommerce_coupon_error.

      add_filter('woocommerce_coupon_error', function ( $message, $error_code, $coupon ) {
      return 'My custom message';
      }, 10, 3 );

      Note that this function accepts 3 params so you can test for error code/coupon to return custom text for specific coupons only.
      Hope that helps!

  2. Hey Bobz! First, thanks for the code.
    Ive tried to implement it on my site with some changes to fit my needs but i cant make it work.
    What I am trying to accomplish is that the coupon code only works if there is more than 1 product in the cart.
    For instance, if there is one product in the cart, it does not work, but when there is two or more it works.
    Thats what I’ve tried, but not successfully so far… :

    add_filter( 'woocommerce_coupon_is_valid', function( $is_valid, $coupon ) {
    if ( in_array( $coupon->get_code() , ['mycouponcode']) && $cart_item['quantity'] > 1)
    return false;
    return $is_valid;
    }, 100, 2 );

    Any ideas on how to make it work?
    Thank you so much in advance!

    1. Hi Carla,

      I’m not sure where are you getting $cart_item variable from.
      However you can access cart using WC()->cart->get_cart(); and then check number of products in the cart.

      Hope that helps


  3. I would like to restrict all coupons to logged-in users (any role doesn’t matter) – we generate a new code each month just for that month. I would not like to have to update the code in functions every time, is there a way to make this work for any coupon always?


    1. If you want to restrict all coupons for logged in users only then all you need to do is: return is_user_logged_in()

Leave a Reply

Your email address will not be published. Required fields are marked *

Please don’t paste any HTML, Js or PHP code into comments box, use or similar service to share code examples.

Characters left: 1000