Viewing 6 replies - 1 through 6 (of 6 total)
  • Hari

    (@harirymera)

    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;
    }
    Thread Starter marcoslmdb

    (@marcoslmdb)

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

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

    • This reply was modified 8 months, 2 weeks ago by marcoslmdb.
    Hari

    (@harirymera)

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

    Thread Starter marcoslmdb

    (@marcoslmdb)

    // 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;

    }

    Thread Starter marcoslmdb

    (@marcoslmdb)

    i found i don’t know why but

    [ as chagne to &#98: on the snippet
    Hari

    (@harirymera)

    Ahh, it’s a unicode character. May I know where did you put the snippet?

    Please refer to this article on how to add snippet to your wordpress site. 🙂
    https://www.wpbeginner.com/beginners-guide/beginners-guide-to-pasting-snippets-from-the-web-into-wordpress/

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Specific user’ is closed to new replies.