Title: Coupon Code
Last modified: June 28, 2021

---

# Coupon Code

 *  Resolved Anonymous User 17335794
 * (@anonymized-17335794)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/coupon-code-24/)
 * Hi,
 * How to coupon code enable to selected user id?
 * For example user id is “abcd123” I want to set coupon code only working to this
   user id only.
    -  This topic was modified 4 years, 10 months ago by Anonymous User 17335794.

Viewing 15 replies - 1 through 15 (of 22 total)

1 [2](https://wordpress.org/support/topic/coupon-code-24/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/coupon-code-24/page/2/?output_format=md)

 *  [Mirko P.](https://wordpress.org/support/users/rainfallnixfig/)
 * (@rainfallnixfig)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/coupon-code-24/#post-14605366)
 * Hi [@anish12345](https://wordpress.org/support/users/anish12345/),
 * You could use some custom code and add a new custom field where to input the 
   user ID under Marketing > Coupons > Select Coupon > Usage restriction.
 * Follow this Stack Overflow question for more details:
 * [https://stackoverflow.com/questions/61572121/add-restriction-to-woocommerce-coupons-by-allowed-user-id](https://stackoverflow.com/questions/61572121/add-restriction-to-woocommerce-coupons-by-allowed-user-id)
 * As a side note, I would recommend using a plugin like [Code Snippets by Code Snippets Pro](https://wordpress.org/plugins/code-snippets/)
   to add custom PHP code into your site without directly accessing the functions.
   php file. Here is a link on how to use the Code Snippets plugin:
 * [https://www.wpbeginner.com/plugins/how-to-easily-add-custom-code-in-wordpress-without-breaking-your-site/](https://www.wpbeginner.com/plugins/how-to-easily-add-custom-code-in-wordpress-without-breaking-your-site/)
 * Hope this helps!
 *  Thread Starter Anonymous User 17335794
 * (@anonymized-17335794)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/coupon-code-24/#post-14605961)
 * I mean not user id restriction.
 * I want give some coupon code to selected customers.
 * How to do this?
 *  Plugin Support [abwaita a11n](https://wordpress.org/support/users/abwaita/)
 * (@abwaita)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/coupon-code-24/#post-14606300)
 * Thanks for getting back.
 * > >I want give some coupon code to selected customers.
 * The [most voted answer](https://stackoverflow.com/a/61572921/6438880) in the 
   [shared Stack Overflow link](https://stackoverflow.com/questions/61572121/add-restriction-to-woocommerce-coupons-by-allowed-user-id)
   seems to allow multiple select customers. It creates a user ID restriction tab
   on the coupon page, where you can list multiple customer IDs.
 * Hope this helps.
    Thanks.
 *  Thread Starter Anonymous User 17335794
 * (@anonymized-17335794)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/coupon-code-24/#post-14606551)
 * It’s not working
 *  Plugin Support [abwaita a11n](https://wordpress.org/support/users/abwaita/)
 * (@abwaita)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/coupon-code-24/#post-14607180)
 * Hi [@anish12345](https://wordpress.org/support/users/anish12345/),
 * While we do not mostly extend our scope of support to custom code, we’d like 
   to see if you added the right code from the SO page. Kindly share with us a screenshot
   of where you added the code.
 * _* I ask because I used the same code and it seems ok
    * I recommend [https://snipboard.io](https://snipboard.io)
   for sharing screenshots – please follow the instructions on the page, then paste
   the URL in this chat. It works with Chrome, Firefox, Safari, and Edge.
 * Thanks.
 *  Thread Starter Anonymous User 17335794
 * (@anonymized-17335794)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/coupon-code-24/#post-14607380)
 * I can see many codes. So I am totally confused.
 * Please share you added code here. That I am copy paste to my snippets plugin.
 *  Plugin Support [abwaita a11n](https://wordpress.org/support/users/abwaita/)
 * (@abwaita)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/coupon-code-24/#post-14607430)
 * Thanks for getting back.
 * The code, which is from the [most voted answer](https://stackoverflow.com/a/61572921/6438880)
   on the Stack Overflow thread, is:
 *     ```
       function action_woocommerce_coupon_options_usage_restriction( $coupon_get_id, $coupon ) {
           woocommerce_wp_text_input( array( 
               'id' => 'customer_user_id',  
               'label' => __( 'User ID restrictions', 'woocommerce' ),  
               'placeholder' => __( 'No restrictions', 'woocommerce' ),  
               'description' => __( 'List of allowed user IDs. Separate user IDs 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 ) {
           // Isset
           if ( isset ( $_POST['customer_user_id'] ) ) {
               $coupon->update_meta_data( 'customer_user_id', sanitize_text_field( $_POST['customer_user_id'] ) );
               $coupon->save();
           }
       }
       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_id = $coupon->get_meta( 'customer_user_id' );
   
           // NOT empty
           if ( ! empty( $customer_user_id ) ) {
               // Convert string to array
               $customer_user_id = explode( ', ', $customer_user_id );
   
               // Get current user id
               $user_id = get_current_user_id();
   
               if ( ! in_array( $user_id, $customer_user_id ) ) {
                   $is_valid = false;
               }
           }
   
           return $is_valid;
       }
       add_filter( 'woocommerce_coupon_is_valid', 'filter_woocommerce_coupon_is_valid', 10, 3 );
       ```
   
 * Hope this helps.
    Thanks.
 *  Thread Starter Anonymous User 17335794
 * (@anonymized-17335794)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/coupon-code-24/#post-14607498)
 * It’s not working. Every login user account showing invalid coupon code.
 * I am totally tried.
 *  [Mirko P.](https://wordpress.org/support/users/rainfallnixfig/)
 * (@rainfallnixfig)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/coupon-code-24/#post-14609217)
 * Hi again,
 * > It’s not working. Every login user account showing invalid coupon code.
 * This is actually working since I’ve tried it on one of my test sites. Did you
   enter the snippet for coupon validation?
 * These are the steps that you’ll have to follow in order for the coupon to work:
 * **1.** Download and install Code Snippets plugin: [https://wordpress.org/plugins/code-snippets/](https://wordpress.org/plugins/code-snippets/)
   **
   2.** Add the following code to Code Snippets to create a new field to the usage
   restriction tab:
 *     ```
       // 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_id',  
               'label' => __( 'User ID restrictions', 'woocommerce' ),  
               'placeholder' => __( 'No restrictions', 'woocommerce' ),  
               'description' => __( 'List of allowed user IDs. Separate user IDs 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 ) {
           // Isset
           if ( isset ( $_POST['customer_user_id'] ) ) {
               $coupon->update_meta_data( 'customer_user_id', sanitize_text_field( $_POST['customer_user_id'] ) );
               $coupon->save();
           }
       }
       add_action( 'woocommerce_coupon_options_save', 'action_woocommerce_coupon_options_save', 10, 2 );
       ```
   
 * **3.**Add a new snippet to Code Snippets for code validation and paste this code:
 *     ```
       // Valid
       function filter_woocommerce_coupon_is_valid( $is_valid, $coupon, $discount ) {
           // Get meta
           $customer_user_id = $coupon->get_meta( 'customer_user_id' );
   
           // NOT empty
           if ( ! empty( $customer_user_id ) ) {
               // Convert string to array
               $customer_user_id = explode( ', ', $customer_user_id );
   
               // Get current user id
               $user_id = get_current_user_id();
   
               if ( ! in_array( $user_id, $customer_user_id ) ) {
                   $is_valid = false;
               }
           }
   
           return $is_valid;
       }
       add_filter( 'woocommerce_coupon_is_valid', 'filter_woocommerce_coupon_is_valid', 10, 3 );
       ```
   
 * **4.** Go to Marketing > Coupons and add a new coupon. Write the User ID under
   Usage restriction. Save the coupon. Use a plugin like [https://wordpress.org/plugins/reveal-ids-for-wp-admin-25/](https://wordpress.org/plugins/reveal-ids-for-wp-admin-25/)
   if you need a quick way to reveal IDs.
 * The coupon assigned to that specific User ID will only work for that user and
   not for other users.
 * Let me know if that helped you out.
    -  This reply was modified 4 years, 10 months ago by [Mirko P.](https://wordpress.org/support/users/rainfallnixfig/).
 *  Thread Starter Anonymous User 17335794
 * (@anonymized-17335794)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/coupon-code-24/#post-14609895)
 * It’s not working. I am totally tried.
 *  Plugin Support [abwaita a11n](https://wordpress.org/support/users/abwaita/)
 * (@abwaita)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/coupon-code-24/#post-14609949)
 * Quite unfortunate to hear [@anish12345](https://wordpress.org/support/users/anish12345/).
 * Just to understand where the challenge lies, kindly share more details about 
   the issues you’re facing with the code.
 * Screenshots of the issue and where you’ve added the code will be highly appreciated.
   You can use snipboard.io to share them here.
 * Thanks.
 *  Thread Starter Anonymous User 17335794
 * (@anonymized-17335794)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/coupon-code-24/#post-14610289)
 * Hi,
 * Please check with attached screenshot.
 * Screenshot 1:- [https://drive.google.com/file/d/1p10ieUSqa9l7ZCvHG2RmbpQ7ZXkeIPly/view?usp=sharing](https://drive.google.com/file/d/1p10ieUSqa9l7ZCvHG2RmbpQ7ZXkeIPly/view?usp=sharing)
 * Screenshot 2:- [https://drive.google.com/file/d/1HVd9qm6xQWHNMPs0cUte8NwqM6eDvKng/view?usp=sharing](https://drive.google.com/file/d/1HVd9qm6xQWHNMPs0cUte8NwqM6eDvKng/view?usp=sharing)
 * Screenshot 3:- [https://drive.google.com/file/d/1l7oM_xkYeBeUg3rrta250MCdWnPsEMJE/view?usp=sharing](https://drive.google.com/file/d/1l7oM_xkYeBeUg3rrta250MCdWnPsEMJE/view?usp=sharing)
 * Screenshot 4:- [https://drive.google.com/file/d/1nJCiz-ZMHzyF418pd7mfvcVG-6Az_ga0/view?usp=sharing](https://drive.google.com/file/d/1nJCiz-ZMHzyF418pd7mfvcVG-6Az_ga0/view?usp=sharing)
 * Screenshot 5:- [https://drive.google.com/file/d/1nIZ5SvE2iCtc5uSXjj7YJ28i3jDjDdof/view?usp=sharing](https://drive.google.com/file/d/1nIZ5SvE2iCtc5uSXjj7YJ28i3jDjDdof/view?usp=sharing)
 * Screenshot 6:- [https://drive.google.com/file/d/1BWX18Uu7Crx2C48qIxuQxCltusCH7D8_/view?usp=sharing](https://drive.google.com/file/d/1BWX18Uu7Crx2C48qIxuQxCltusCH7D8_/view?usp=sharing)
 *  Plugin Support [abwaita a11n](https://wordpress.org/support/users/abwaita/)
 * (@abwaita)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/coupon-code-24/#post-14610998)
 * Thanks for the screenshots!
 * The code seems okay, and I see that the problem probably starts on image 4.
    
   Link to image: [https://drive.google.com/file/d/1nJCiz-ZMHzyF418pd7mfvcVG-6Az_ga0/view](https://drive.google.com/file/d/1nJCiz-ZMHzyF418pd7mfvcVG-6Az_ga0/view)
 * That **“User ID restrictions”** field expects an ID, not the username. You’ve
   provided a username.
 * Here’s how to find a user ID on WordPress: [https://www.wpbeginner.com/beginners-guide/how-to-find-post-category-tag-comments-or-user-id-in-wordpress/](https://www.wpbeginner.com/beginners-guide/how-to-find-post-category-tag-comments-or-user-id-in-wordpress/)
 * *Note that by default, the ID of the built-in WordPress administrator account
   is 1.
 * Hope this helps.
    Thanks.
 *  Thread Starter Anonymous User 17335794
 * (@anonymized-17335794)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/coupon-code-24/#post-14611742)
 * It’s working now. But I am not satisfied. Because I want to set customer user
   name or email id.
 * So Please help me.
 *  [Mirko P.](https://wordpress.org/support/users/rainfallnixfig/)
 * (@rainfallnixfig)
 * [4 years, 10 months ago](https://wordpress.org/support/topic/coupon-code-24/#post-14613637)
 * Hi again,
 * > It’s working now
 * Glad to know that it’s now working with the User ID. You can also restrict by
   Email address that can use a coupon. The system will verify against the customer’s
   billing email.
 * Go to Marketing > Coupons > Select Coupon > Usage restriction > Allowed emails.
 * 
    Link to image: [https://i.imgur.com/TeVQUCX.png](https://i.imgur.com/TeVQUCX.png)
 * If you wish to filter by username that would imply further customization that
   it’s outside the support of scope we can offer on this forum.
 * If you require further assistance with that, you could hire a developer to help
   create that portion of the code. If you would like to investigate that possibility
   further, you could reach out to some of the official WooCommerce development 
   partners via this link below:
 * [https://woocommerce.com/customizations/](https://woocommerce.com/customizations/)
 * Hope this helps!

Viewing 15 replies - 1 through 15 (of 22 total)

1 [2](https://wordpress.org/support/topic/coupon-code-24/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/coupon-code-24/page/2/?output_format=md)

The topic ‘Coupon Code’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 22 replies
 * 3 participants
 * Last reply from: [Mirko P.](https://wordpress.org/support/users/rainfallnixfig/)
 * Last activity: [4 years, 10 months ago](https://wordpress.org/support/topic/coupon-code-24/page/2/#post-14660369)
 * Status: resolved