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->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:

Misha Rudrastyh

Misha Rudrastyh

I develop websites since 2008, so it is total of 13 years of experience, oh my gosh. Most of all I love love love to create websites with WordPress and Gutenberg, some ideas and thoughts I share throughout my blog.

Need some developer help? Contact me