How to display available payment gateways for your WooCommerce website

How to display available payment gateways for your WooCommerce website

Whether you’re a seasoned e-commerce business owner or a new entrant in the online selling arena, managing payment gateways on your WooCommerce website is a critical aspect of your business operations. This comprehensive guide will delve into how you can display, enable, and disable payment gateways on your WooCommerce platform using simple yet effective code.

Display available payment gateways

Having a WooCommerce platform means having a multitude of payment gateways at your disposal. However, effectively managing these payment options is key to ensuring seamless transactions and offering your customers flexibility. This article will shed light on how you can display, enable, and disable these payment gateways on your website.

Let’s start by understanding how to display the available payment gateways. WooCommerce offers an extensive array of gateways, but not all may be relevant or required for your specific business. To streamline your operations, you may want to display only a selected few. Here’s how you can do it:

Firstly, you need to call the ‘WC()’ function and access the ‘payment_gateways’ object to get the available gateways. We store these gateways in a variable called ‘$available_gateways’ and ‘$gateways’. Here’s the initial part of the code:

<?php
	$available_gateways = WC()->payment_gateways->get_available_payment_gateways();
	$gateways = WC()->payment_gateways->get_available_payment_gateways();
?>

We then create an empty array called ‘$enabled_gateways’ to hold our enabled payment gateways. If there are any gateways, we loop through each of them, checking if they are enabled. If they are, we add them to our array. Here’s how we do it:

<?php
	$enabled_gateways = [];
	if( $gateways ) {
	    foreach( $gateways as $gateway ) {
	        if( $gateway->enabled == 'yes' ) {
	            $enabled_gateways[] = $gateway;
	        }
	    }
	}
?>

To print this list, we simply call the ‘print_r()’ function:

<?php
	echo '<pre>';
	print_r( $enabled_gateways ); // Should return an array of enabled gateways
	echo '</pre>';
?>

Enable or disable the payment gateways:

Once you understand how to display the payment gateways, you might also need to enable or disable specific gateways according to your business needs. Here’s how you can filter gateways based on your preferences:

We start by defining a function, ‘filter_gateways()’. This function takes an argument, ‘$gateways’. Within this function, we first fetch the global variable ‘$woocommerce’. We then use the ‘rand()’ function to generate a random number between 1 and 10 and store it in ‘$thewinner’. Based on the value of ‘$thewinner’, we unset certain gateways. Here’s the full function:

<?php
	function filter_gateways($gateways){
		global $woocommerce;
		$thewinner = rand(1,10);
		if ($thewinner == 1 ) {
		    unset($gateways['authorize_net_aim_emulation']);
		} else{
		    unset($gateways['authorize_net_aim_echeck']);
		    unset($gateways['authorize_net_aim']);
		}
		return $gateways;
	}
	add_filter('woocommerce_available_payment_gateways','filter_gateways');
?>

By understanding these functions and utilizing the potential of WooCommerce, you can provide a seamless and flexible shopping experience for your customers. The power to manage your e-commerce platform effectively lies in your hands. Embrace these codes, and watch your WooCommerce website transform!

Related Posts