• Resolved Anonymous User 17335794

    (@anonymized-17335794)


    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)
  • Mirko P.

    (@rainfallnixfig)

    Hi @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

    As a side note, I would recommend using a plugin like Code Snippets by Code Snippets Pro 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/

    Hope this helps!

    Thread Starter Anonymous User 17335794

    (@anonymized-17335794)

    I mean not user id restriction.

    I want give some coupon code to selected customers.

    How to do this?

    Plugin Support abwaita a11n

    (@abwaita)

    Thanks for getting back.

    >I want give some coupon code to selected customers.

    The most voted answer in the shared Stack Overflow link 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)

    It’s not working

    Plugin Support abwaita a11n

    (@abwaita)

    Hi @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 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)

    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

    (@abwaita)

    Thanks for getting back.

    The code, which is from the most voted answer 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)

    It’s not working. Every login user account showing invalid coupon code.

    I am totally tried.

    Mirko P.

    (@rainfallnixfig)

    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/
    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/ 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..
    Thread Starter Anonymous User 17335794

    (@anonymized-17335794)

    It’s not working. I am totally tried.

    Plugin Support abwaita a11n

    (@abwaita)

    Quite unfortunate to hear @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)

    Plugin Support abwaita a11n

    (@abwaita)

    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

    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/

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

    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.

    (@rainfallnixfig)

    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

    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/

    Hope this helps!

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

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