Add restriction to WooCommerce coupons by allowed user ID



PHP Snippet 1:

$user_id = get_current_user_id();
update_post_meta( $new_coupon_id, 'couponuserid', $user_id );

PHP Snippet 2:

add_filter( 'woocommerce_coupon_is_valid', function( $is_valid, $coupon ) {

    /**
     * Selected coupons allowed only for selected User IDs
     */
    $is_userid_coupon = get_post_meta( $coupon->get_id(), 'couponuserid', true );
    if ( isset( $is_userid_coupon ) ) {
        $user = wp_get_current_user();
        if ( $user->ID && $user->ID == get_post_meta( $coupon->get_id(), 'couponuserid', true ) ) {
            return true;
        }
    }

    return $is_valid;
}, 100, 2 );