Here’s how you can define payment options by user role in WooCommerce.

Marc Wagner, July 22, 2022

You don’t always want all visi­tors to be able to use all pay­ment methods. Espe­ci­al­ly if they are new cus­to­mers, you are used to being a bit more cau­tious. By default, howe­ver, only all pay­ment methods can be glo­bal­ly acti­va­ted or deac­ti­va­ted.

This can be con­ve­ni­ent­ly cus­to­mi­zed with just a few lines of code.

The finished code #

We start as usu­al with the com­ple­te code, which you can sim­ply add to functions.php.

<?php
function setPaymentOptionsByUserRole( $availablePaymentGateways ) {
    if ( is_user_logged_in() ) {

        $allowedPaymentGateways = array();
        $user = wp_get_current_user();

        if ( array_intersect( array('merchant'), $user->roles )) {
            $allowedPaymentGateways = array(
                'bacs',
                'direct-debit',
                'ppec_paypal',
                'paypal_plus',
            );
        }

        if(!empty($allowedPaymentGateways)) {
            foreach ( $availablePaymentGateways as $slug => $gateway ) {
                if ( ! in_array( $slug, $allowedPaymentGateways ) ) {
                    unset( $availablePaymentGateways[ $slug ] );
                }
            }
        }
    }else{
        /*
         * We can use the else loop to change the payment gateways for 
         * guests.
         */
        $allowedPaymentGateways = array(
            'bacs',
            'direct-debit',
            'ppec_paypal',
            'paypal_plus',
        );

        if(!empty($allowedPaymentGateways)) {
            foreach ( $availablePaymentGateways as $slug => $gateway ) {
                if ( ! in_array( $slug, $allowedPaymentGateways ) ) {
                    unset( $availablePaymentGateways[ $slug ] );
                }
            }
        }
    }

    return $availablePaymentGateways;
}

add_filter('woocommerce_available_payment_gateways', 'setPaymentOptionsByUserRole', 90, 1);

The who­le thing can be exten­ded end­less­ly. In our exam­p­le we have limi­t­ed the pay­ment methods for all users with the user role “mer­chant” to Pay­Pal, bank trans­fer, direct debit and cre­dit card.

if ( array_intersect( array('merchant'), $user->roles )) {
	$allowedPaymentGateways = array(
		'bacs',
		'direct-debit',
		'ppec_paypal',
		'paypal_plus',
	);
}

All other pay­ment methods will be remo­ved after­wards.

if(!empty($allowedPaymentGateways)) {
	foreach ( $availablePaymentGateways as $slug => $gateway ) {
		if ( ! in_array( $slug, $allowedPaymentGateways ) ) {
			unset( $availablePaymentGateways[ $slug ] );
		}
	}
}

This is how you can restrict additional roles #

You can add more roles by copy­ing and pas­ting the fol­lo­wing block direct­ly below.

if ( array_intersect( array('<user_role>'), $user->roles )) {
	$allowedPaymentGateways = array();
}

Replace ” with the desi­red user role.

Word­Press user roleSlug
Edi­toredi­tor
Aut­horaut­hor
Admi­nis­tra­toradmi­nis­tra­tor
Con­tri­bu­torcon­tri­bu­tor
Sub­scri­bersub­scri­ber
Word­Press’ user roles

That’s about it. With a few lines of code, you can easi­ly cus­to­mi­ze the pay­ment pro­cess. I hope this artic­le was hel­pful for you. If you have any ques­ti­ons, we will be hap­py to ans­wer them for you.

Avatar of Marc Wagner
Marc Wagner

Hi Marc here. I'm the founder of Forge12 Interactive and have been passionate about building websites, online stores, applications and SaaS solutions for businesses for over 20 years. Before founding the company, I already worked in publicly listed companies and acquired all kinds of knowledge. Now I want to pass this knowledge on to my customers.

Similar Topics

Comments

Leave A Comment