Filter Payment Methods by Cart Amount
Different payment methods make sense for different order values. You might want to encourage smaller purchases through one payment method while reserving PayPal for larger transactions, or perhaps you want to avoid processing fees on very small orders by limiting payment options. Whatever your business logic, you can programmatically control which payment methods appear based on the customer's cart total.
This approach gives you complete control over the checkout experience based on order value. When a customer's cart doesn't meet your specified criteria, the payment method simply won't appear as an option, creating a cleaner, more targeted checkout flow.
When would you use this?
Filtering payment methods by cart amount is particularly useful for merchants who want to optimize their payment processing costs or create strategic checkout experiences. You might want to encourage higher-value transactions through specific payment methods, or avoid offering certain payment options for small purchases where processing fees would eat into your margins.
Common use cases include setting minimum order amounts for PayPal to reduce transaction fees on small purchases, offering premium payment methods only for high-value orders, or steering customers toward preferred payment methods based on order size.
The Code
This example removes PayPal as a payment option when the cart total is less than $100. The code uses WooCommerce's core filter to modify the available payment gateways before they're displayed to the customer. Add this code to your theme's functions.php
file or a custom plugin:
add_filter('woocommerce_available_payment_gateways', function($gateways){
if(is_checkout() || is_cart()){
if(WC()->cart->get_total('edit') < 100){
unset($gateways['ppcp']);
}
}
return $gateways;
});
How the Code Works
The code hooks into WooCommerce's woocommerce_available_payment_gateways
filter, which runs every time WooCommerce determines which payment methods to show the customer. First, it checks if the current page is either the checkout or cart page using is_checkout()
and is_cart()
functions - this ensures the filter only runs where payment methods are actually displayed.
Next, it retrieves the cart total using WC()->cart->get_total('edit')
. The 'edit' parameter gets the raw total without any formatting, making it perfect for numerical comparisons. If the cart total is less than $100, the code removes the PayPal gateway by unsetting $gateways['ppcp']
from the available gateways array.
The gateway identifier ppcp
specifically targets the Payment Plugins for PayPal WooCommerce plugin's main PayPal payment method. You can replace this with any gateway ID to filter different payment methods.
Customizing the Amount Threshold
You can easily adjust the minimum amount by changing the comparison value:
// Set minimum to $50
if(WC()->cart->get_total('edit') < 50){
unset($gateways['ppcp']);
}
// Set minimum to $250
if(WC()->cart->get_total('edit') < 250){
unset($gateways['ppcp']);
}
Filtering Multiple Payment Methods
You can remove multiple payment gateways based on the same condition:
add_filter('woocommerce_available_payment_gateways', function($gateways){
if(is_checkout() || is_cart()){
if(WC()->cart->get_total('edit') < 100){
unset($gateways['ppcp']); // PayPal
unset($gateways['ppcp_cc']); // PayPal Credit Cards
}
}
return $gateways;
});
Setting Maximum Amounts
You can also create maximum thresholds to limit payment methods for high-value orders:
add_filter('woocommerce_available_payment_gateways', function($gateways){
if(is_checkout() || is_cart()){
// Remove PayPal for orders over $5,000
if(WC()->cart->get_total('edit') > 5000){
unset($gateways['ppcp']);
}
}
return $gateways;
});
Creating Amount Ranges
For more complex scenarios, you can create amount ranges where certain payment methods are only available within specific price brackets:
add_filter('woocommerce_available_payment_gateways', function($gateways){
if(is_checkout() || is_cart()){
$cart_total = WC()->cart->get_total('edit');
// Only show PayPal for orders between $100 and $1,000
if($cart_total < 100 || $cart_total > 1000){
unset($gateways['ppcp']);
}
}
return $gateways;
});
Important Considerations
Remember that filtering payment methods affects the customer experience significantly. If you remove all payment methods, customers won't be able to complete their purchase. Always ensure at least one payment method remains available for any cart total scenario.
The cart total used in this code includes taxes and shipping costs if they've been calculated. If you need to filter based on subtotal only, use WC()->cart->get_subtotal()
instead.
Testing Your Implementation
To verify the code is working correctly, add items to your test cart to reach different total amounts and observe which payment methods appear at checkout. Test with amounts both above and below your threshold to confirm the filtering works as expected.
Make sure to test on both the cart and checkout pages if your site displays payment methods on both locations. The code accounts for both scenarios by checking is_checkout() || is_cart()
.
Finding Gateway Identifiers
If you need to filter other payment gateways, you can find their identifiers by adding this temporary debugging code:
add_filter('woocommerce_available_payment_gateways', function($gateways){
error_log(print_r(array_keys($gateways), true));
return $gateways;
});
This will log all available gateway IDs to your error log, helping you identify the correct identifiers for other payment methods you want to filter.