Are you looking to hide payment methods based on the billing pin code on the WooCommerce checkout page, then the snippet below is the solution.
add_filter( 'woocommerce_available_payment_gateways', function( $available_gateways ) { // if Cash on Delivery is already disabled, let's exit the function if( empty( $available_gateways['cod'] ) ) { return $available_gateways; } // get customer object $customer = WC()->customer; // check if customer object is not null if ( is_object( $customer ) ) { // get postal code $postal_code = $customer->get_billing_postcode(); // deactivate payment method if( in_array( $postal_code, array( '415709', '410208' ) ) ) { unset( $available_gateways['cod'] ); } } return $available_gateways; } );
Output
In the below output, the code snippet checks if “Cash on Delivery” is enabled. If it is, it looks at the customer’s billing postal code. If the postal code matches one of the specified values (in this case, ‘415709’ or ‘410208’), it disables the “Cash on Delivery” payment method.
Furthermore, you can also validate shipping zone code during the checkout in WooCommerce.