The code helps to customize the WooCommerce checkout page by dynamically disabling a certain payment gateway when a specific shipping method is chosen.
add_filter( 'woocommerce_available_payment_gateways', 'ts_gateway_disable_for_shipping_rate' ); function ts_gateway_disable_for_shipping_rate( $available_gateways ) { if ( ! is_admin() && WC()->session ) { $chosen_methods = WC()->session->get( 'chosen_shipping_methods' ); $chosen_shipping = $chosen_methods[0]; if ( isset( $available_gateways['cod'] ) && 0 === strpos( $chosen_shipping, 'local_pickup' ) ) { unset( $available_gateways['cod'] ); } } return $available_gateways; }
Output
The code disables ‘Cash on Delivery’ when ‘local pickup’ is chosen as their preferred shipping method during checkout. Irrespective of any zone the particular payment gateway is disabled here.
Disabling payment gateways can be done on various conditions. As done in the above criteria, you can also disable payment method for specific category WooCommerce checkout.