Restrict Payment Methods based on Cart Total
In this tutorial I will show you how to disable WooCommerce payment gateways based on cart total without use of any plugin.
If you need some custom coding help with WooCommerce payment gateways or maybe you need to develop one – please feel free to contact me in both cases.
In the below example we are going to disable “Cash on Delivery” payment method if total amount in cart is more than 1000 (in shop currency) using woocommerce_available_payment_gateways
filter hook.
add_filter( 'woocommerce_available_payment_gateways', 'rudr_turn_off_cod' );
function rudr_turn_off_cod( $available_gateways ) {
if( is_admin() ) {
return $available_gateways;
}
// STEP 1: Get order/cart total
if( is_wc_endpoint_url( 'order-pay' ) ) { // Pay for order page
$order_id = wc_get_order_id_by_order_key( $_GET[ 'key' ] );
$order = wc_get_order( $order_id );
$order_total = $order->get_total();
} else { // Cart/Checkout page
$order_total = WC()->cart->get_total();
}
// STEP 2: Disable payment gateway if order/cart total is more than 1000
if ( $order_total > 1000 ) {
unset( $available_gateways[ 'cod' ] ); // unset Cash on Delivery
}
return $available_gateways;
}
Notes:
WC()->cart->get_total()
includes not only the total amount of products in the cart but also the shipping costs and fees, if you do not need it, useWC()->cart->get_cart_subtotal()
.cod
is the slug of Cash on Delivery payment method. Where to get the other payment method slugs? First of all, you can always useprint_r()
, second, here are some default payment methods slugs:bacs
(Direct bank transfer),cheque
(Check payments) andpaypal
and third, you can add a column with ID into payment methods table in WooCommerce settings.- On line 10 of this snippet you’ve probably noticed
is_wc_endpoint_url()
condition, we need it because we shouldn’t forget about “Pay for order” page in My Account, our payment method restrictions should on this page as well, right? - You can insert the code to your current (or child) theme
functions.php
file.

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
Hello,
Thank you for great tutorial.
I have changed one pice of code as you have mentioned before to use “WC()->cart->subtotal” to get amount without tax and shipping but when i change that i get a lot of PHP Warning in my error_log.
PHP Warning: Attempt to read property “subtotal” on null on line 30
PHP Warning: Attempt to read property “subtotal” on null on line 30
PHP Warning: Attempt to read property “subtotal” on null on line 30
On line 30
Any idea how to fix this? I am using PHP 8.1 version in WP.
I’ve a PHP Warning at line 17:
Attempt to read property “total” on null…
have a fix?
TY
Updated the tutorial