How to Get and Hook WooCommerce Payment Gateways

In this post I want to share with you a couple of useful functions to work with payment gateways in your WooCommerce store.

/23 comments
All the installed WooCommerce payment gateways under the Checkout tab in the shop settings.
You can find all the installed payment gateways in WooCommerce > Settings, under the Checkout tab. Here on the screenshot you can see Stripe, PayPal, PayPal Website Payments Pro etc.

All the below functions and hooks you can find in woocommerce/includes/class-wc-payment-gateways.php.

Get Payment Gateways

Here I will show you how to work with two class methods WC()->payment_gateways->payment_gateways() and WC()->payment_gateways->payment_gateways().

All the installed payment methods

By default WooCommerce has 4 installed payment gateways: BACS, Check Payments, Cash on Delivery and PayPal. The additional payment methods can be installed with plugins. Usually if plugin is activated = the gateway is installed and you can find it in the shop settings.

$installed_payment_methods = WC()->payment_gateways->payment_gateways();

This class function returns objects of all payment gateways installed on your WooCommerce store. What is inside objects? Just print_r() the result and you’ll know.

$installed_payment_methods = WC()->payment_gateways->payment_gateways();
print_r( $installed_payment_methods );

In the below example let’s try to print the names of payments gateways:

$installed_payment_methods = WC()->payment_gateways->payment_gateways();
 
foreach( $installed_payment_methods as $method ) {
	echo $method->title . '<br />';
}

This function also has a filter woocommerce_payment_gateways. This filter runs on the early stage and allows you to completely remove any of WooCommerce default payment gateways. Read below how.

Only available gateways

WooCommerce uses this function when it gets the payment methods directly on the checkout page and while processing payment.

So, for example, if a specific payment method is not enabled in the shop settings, it won’t be shown.

$available_payment_methods = WC()->payment_gateways->get_available_payment_gateways();

The return result is similar to WC()->payment_gateways->payment_gateways(). It also has a filter woocommerce_available_payment_gateways, described below.

How to remove WooCommerce default payment gateways from everywhere: BACS, Check Payments, Cash on Delivery and PayPal

function misha_remove_default_gateway( $load_gateways ){
 
	unset( $load_gateways[0] ); // WC_Gateway_BACS
	unset( $load_gateways[1] ); // WC_Gateway_Cheque
	unset( $load_gateways[2] ); // WC_Gateway_COD (Cash on Delivery)
	//unset( $load_gateways[3] ); // WC_Gateway_Paypal
 
	return $load_gateways;
}
 
add_filter( 'woocommerce_payment_gateways', 'misha_remove_default_gateway', 10, 1 );

Insert the code into your current theme functions.php and go to the shop checkout settings.

Remove WooCommerce default payment gateways: BACS, Check Payments, Cash on Delivery.
As the result you can see that only PayPal method is remained.

How to force allow a specific payment gateway when no gateways are available

When you could need this?

Let me describe the situation from life. My client has the subscription-based website with WooCommerce Memberships and WooCommerce Subscriptions plugins installed. As a payment gateway we used Stripe. But it happened that Stripe stopped working with his business. After that we deactivate Stripe plugin, and even if we have another payment gateways installed, WooCommerce doesn’t allow the existing subscribers to renew their subscriptions with another payment gateway. And this was unacceptable.

The following small piece of code saved our lives.

/*
 * if no methods are allowed, allow PayPal Payflow
 */
function misha_change_wc_gateway_if_empty( $allowed_gateways ){
 
	if( empty( $allowed_gateways ) ) {
		$allowed_gateways = array();
		$all_gateways = WC()->payment_gateways->payment_gateways();
		$allowed_gateways['paypal_pro_payflow'] = $all_gateways['paypal_pro_payflow'];
	}
 
	return $allowed_gateways;
 
}
add_filter('woocommerce_available_payment_gateways','misha_change_wc_gateway_if_empty', 9999, 1 );

More about WooCommerce Checkout

Misha Rudrastyh

Misha Rudrastyh

I love WordPress, WooCommerce and Gutenberg so much. 10 yrs of experience.

Need some custom developer help? Let me know

Follow Misha

Need some help with WooCommerce?

If you need some professional developer help, I will be happy to assist you.

Contact me Who I am?

Comments — 23

Leave a comment

php js HTML CSS Code