In this woocommerce setup, I have 2 Payment methods, Paypal and Cash on Delivery.
Now how can Cash on Delivery be hidden/disabled for certain Postal codes only.
This is the code I found on Gist
// Disable gateway based on country
function payment_gateway_disable_country( $available_gateways ) {
global $woocommerce;
if ( isset( $available_gateways['ccavenue'] ) && $woocommerce->customer->get_country() <> 'IN' ) {
unset( $available_gateways['ccavenue'] );
} else if ( isset( $available_gateways['paypal'] ) && $woocommerce->customer->get_country() == 'IN' ) {
unset( $available_gateways['paypal'] );
}
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_country' );
To disable/hidden “Cash on Delivery”, Place this code in your theme’s function.php .
For more detail: woocommerce-hide-payment-gatway-based-on-visitors-country
In the “checkout page” user can have two addresses – billing and shipping one.
To work correctly only with changes of Shipping one if it’s filled I changed a bit the code. You have to test shipping countrycode if it’s set, if not just user countrycode:
In the code above you used country code to disable payment gateway by, but you mentioned you would like to do it by Postal Code.
You’re right about using
woocommerce_available_payment_gateways
, but instead of using$woocommerce->customer->get_country()
you have to useWC()->customer->get_shipping_postcode()
(orWC()->customer->get_billing_postcode()
for some situations).You mentioned PayPal and Cash on Delivery payment gateway, we need their IDs, there are
paypal
andcod
accordingly.In the code below let’s deactivate Cash on Delivery for a couple postal codes, for example ‘1234’ and ‘5678’:
Code can be inserted to your current theme functions.php file or a custom plugin. More info you can find in this tutorial: https://rudrastyh.com/woocommerce/hide-payment-methods-based-on-postal-code.html