mirror of
https://gh.wpcy.net/https://github.com/mainwp/Code-Snippets-Functions.git
synced 2026-04-29 11:32:21 +08:00
https://stackoverflow.com/questions/76381506/enable-cod-payment-method-only-for-specific-countries-in-woocommerce/76384120
12 lines
611 B
Text
12 lines
611 B
Text
add_filter( 'woocommerce_available_payment_gateways', 'countries_based_payment_gateway_cod' );
|
|
function countries_based_payment_gateway_cod( $available_gateways ) {
|
|
if ( is_admin() ) return $available_gateways; // Only on frontend
|
|
|
|
// HERE define the allowed country codes below (array of coma separated strings)
|
|
$allowed_countries = array( 'AE' ); // United Arab Emirates country code
|
|
|
|
if ( isset( $available_gateways['cod'] ) && ! in_array( WC()->customer->get_shipping_country(), $allowed_countries ) ) {
|
|
unset( $available_gateways['cod'] );
|
|
}
|
|
return $available_gateways;
|
|
}
|