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

Marc Wag­ner

Novem­ber 11, 2020

2 min read|

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

Tha­t’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.

88e86fcb816eff22bc917094df2862d8dd5c0e978b333e6dd5f36f808990c261 96

Arti­kel von:

Marc Wag­ner

Hi Marc here. I’m the foun­der of Forge12 Inter­ac­ti­ve and have been pas­sio­na­te about buil­ding web­sites, online stores, appli­ca­ti­ons and SaaS solu­ti­ons for busi­nesses for over 20 years. Befo­re foun­ding the com­pa­ny, I alre­a­dy work­ed in publicly lis­ted com­pa­nies and acqui­red all kinds of know­ledge. Now I want to pass this know­ledge on to my cus­to­mers.

Hast du eine Fra­ge? Hin­ter­lass bit­te einen Kom­men­tar