How to Remove Payment Gateways in WooCommerce by Cart Total
WooCommerce is an extremely flexible e-commerce platform that allows store owners to customize almost every aspect of their store. One common customization is the ability to enable or disable payment gateways based on certain criteria, such as the cart total amount.
For instance, you may want to deactivate a specific payment gateway if the cart total amount is below a certain threshold, or if it exceeds a certain limit. Fortunately, WooCommerce offers a flexible API that enables such customizations.
Below, I’ll show you how you can remove a payment gateway in WooCommerce based on the cart total amount using custom PHP code.
Identify Payment Gateways and Their Identifiers
The first thing you need to do is identify the payment gateways you want to control and obtain their identifiers. You can find these identifiers in your WooCommerce store settings.
Write Custom PHP Code
Next, we’ll write the custom PHP code that will remove payment gateways based on the cart total amount. You can place this code in the functions.php
file of your active theme or in a custom plugin.
add_filter( 'woocommerce_available_payment_gateways', 'custom_disable_payment_gateway_by_total_amount' ); function custom_disable_payment_gateway_by_total_amount( $available_gateways ) { // Get the cart total amount $cart_total = WC()->cart->get_total(); // Define the amount limit to disable the payment gateway $limit_amount = 100; // Change to the desired amount // Check if the cart total amount is less than the limit if ( $cart_total < $limit_amount ) { // Remove the payment gateway based on its identifier unset( $available_gateways['payment_gateway_name'] ); } return $available_gateways; }
Make sure to replace 'payment_gateway_name'
with the actual identifier of the payment gateway you want to disable. You can repeat this unset
block for each payment gateway you want to control.
Customize Amount Limits
In the above code, we’ve set a $100 amount limit as an example. You should change this amount according to your specific needs. You can set different amount limits and actions for each payment gateway based on your business requirements.
Customizing payment gateways in WooCommerce based on the cart total amount can help you better control the available payment options for your customers and optimize their shopping experience. With the flexibility of the WooCommerce API and a bit of custom PHP code, you can easily implement this functionality in your online store.