Specific user | WordPress.org

[ad_1]

Hi @marcoslmdb,

Yes, it’s possible to set the Invoice Gateway to specific user. You can either set the payment method exclusive to user roles or user IDs. I’ll send you both snippet so that you can choose based on your needs 🙂

Don’t forget to change the values in $allowed_roles array for restricting based on user roles, and $allowed_user_ids for restricting based on user IDs.

Set Invoice Gateway payment method for specific roles

// Invoice payment gateway only available for specific roles
add_filter( 'woocommerce_available_payment_gateways', 'igfw_restrict_to_specific_roles' );
function igfw_restrict_to_specific_roles( $available_gateways ) {

    $allowed_roles = array( 
        'role_1',
        'role_2',
        'role_3',
    );

    if ( ! is_user_logged_in() ) {
        unset( $available_gateways['igfw_invoice_gateway'] );
    } else {
        $user = wp_get_current_user();
        $user_roles = ( array ) $user->roles;
        if ( empty( array_intersect( $allowed_roles, $user_roles ) ) ) {
            unset( $available_gateways['igfw_invoice_gateway'] );
        }
    }

    return $available_gateways;
}

Set Invoice Gateway payment method for specific user IDs

// Invoice payment gateway only available for specific user IDs
add_filter( 'woocommerce_available_payment_gateways', 'igfw_restrict_to_specific_user_ids' );
function igfw_restrict_to_specific_user_ids( $available_gateways ) {

    $allowed_user_ids = array( 
        1,
        2,
        3,
    );

    if ( ! is_user_logged_in() ) {
        unset( $available_gateways['igfw_invoice_gateway'] );
    } else {
        $user = wp_get_current_user();
        $user_id = $user->ID;
        if ( ! in_array( $user_id, $allowed_user_ids ) ) {
            unset( $available_gateways['igfw_invoice_gateway'] );
        }
    }

    return $available_gateways;
}

Hi i have this error when i try to activate the snippet

Syntax error, unexpected token “&”, expecting “->” or “?->” or “{” or “[“.

  • This reply was modified 2 hours, 23 minutes ago by marcoslmdb.

How weird, it works fine on my end. Please paste the snippet that you’ve been edited here?

// Invoice payment gateway only available for specific user IDs
add_filter( ‘woocommerce_available_payment_gateways’, ‘igfw_restrict_to_specific_user_ids’ );
function igfw_restrict_to_specific_user_ids( $available_gateways ) {

$allowed_user_ids = array(
    546,
    541,
);

if ( ! is_user_logged_in() ) {
    unset( $available_gateways['igfw_invoice_gateway'] );
} else {
    $user = wp_get_current_user();
    $user_id = $user->ID;
    if ( ! in_array( $user_id, $allowed_user_ids ) ) {
        unset( $available_gateways['igfw_invoice_gateway'] );
    }
}

return $available_gateways;

}

i found i don’t know why but

[ as chagne to &#98: on the snippet

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer