WooCommerce is a shopping store of WordPress. WooCommerce Payment Gateways helps to enable payment options for your website.
Table of Contents
Introduction
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 help with how you can display, enable, and disable the WooCommerce payment gateways using simple yet effective techniques.
Understanding WooCommerce Payment Gateways
Before we dive into the specifics of displaying payment gateways, let’s grasp the fundamental concept. WooCommerce payment gateways are tools that enable the processing of financial transactions on your online store. These gateways connect your store to payment processors, allowing customers to make purchases using their preferred payment methods.
Display available WooCommerce 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>'; ?>
For example, after executing the code, you will be able to get the array $enabled_gateways
displayed as follows:
Enable or disable the WooCommerce 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'); ?>
Troubleshooting Common WooCommerce Payment Gateway Display Issues
- Check Gateway Settings:
- Ensure that the payment gateways you want to display are activated in the WooCommerce settings. Deactivate any unnecessary gateways to simplify the checkout process.
- Theme Compatibility:
- Some themes might conflict with the default display of payment gateways. Verify the compatibility of your theme with WooCommerce and consider reaching out to theme support for assistance.
- Plugin Conflicts:
- Conflicts with other plugins can impact the proper display of payment gateways. Temporarily deactivate plugins one by one to identify the conflicting plugins and seek a resolution.
Conclusion
By understanding these functions and utilizing the potential of WooCommerce Payment Gateways, 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!