How to Get Payment Gateways Programmatically

Looking for WooCommerce payment plugin development? Let me help you.

First of all, if you go to “WooCommerce > Settings > Payments”, you can find all the installed payment gateways there.

All the installed WooCommerce payment gateways under the Checkout tab in the shop settings.
Here on the screenshot you can see Direct bank transfer, check payments, cash on delivery, PayPal and my custom gateway. By the way you can add a column with ID using this tutorial.

Here I will show you how to work with two class methods:

  • WC()->payment_gateways->payment_gateways(),
  • and WC()->payment_gateways->get_available_payment_gateways()

What is the difference? 🤔 You will find out in a second.

payment_gateways() – Allows to Get 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.

This class method returns objects of all payment gateways installed on your WooCommerce store. Want to get custom payment gateway data? Just print_r() the result and you’ll have it.

$installed_payment_methods = WC()->payment_gateways->payment_gateways();
echo '<pre>'; print_r( $installed_payment_methods ); echo '</pre>';

Here is my example:

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

Some of WC_Payment_Gateway methods will also be available, example:

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

foreach( $installed_payment_methods as $method ) {
	echo $method->get_title() . '<br />'; // print methods titles
}

You can also find some mysterious “Misha Gateway” on my screenshots 🙃 Okay, it is a custom payment gateway I created in a separate tutorial.

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.

get_available_payment_gateways() – Get 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.

Get Selected Payment Method

Here is how you can get a payment method slug (ID) from user session.

$selected_payment_method_id = WC()->session->get( 'chosen_payment_method' );
// 'bacs', 'cod', 'cheque', 'paypal' etc

Ok now, but how to get the selected payment gateway title or other custom data?

$selected_payment_method_id = WC()->session->get( 'chosen_payment_method' );
$selected_payment_method = WC()->payment_gateways->payment_gateways()[ $selected_payment_method_id ];
// print_r( $selected_payment_method );
Misha Rudrastyh

Misha Rudrastyh

I develop websites since 2008, so it is total of 13 years of experience, oh my gosh. Most of all I love love love to create websites with WordPress and Gutenberg, some ideas and thoughts I share throughout my blog.

Need some developer help? Contact me

Follow me on Twitter