Enable COD payment method only for specific countries in WooCommerce

377 views Asked by At

I have try to enable COD (Cash on delivery payment method) for UAE country (United Arab Emirates), but it can't show in checkout page.

My current payment gateway with network international, accepting debit card and credit card but I also want to enable COD payment for UAE only. Please tell me any functions.php custom code for enabling COD for UAE only.

Any help is appreciated.

1

There are 1 answers

0
LoicTheAztec On

The following code snippet will enable Cash on delivery payment method (COD) only for specific defined country(ies) code(s):

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;
}

Code goes in functions.php file of your active child theme (or active theme). It should work.

If needed you can replace in the code

WC()->customer->get_shipping_country()

with the billing country:

WC()->customer->get_billing_country()

Based on: Show Cash on delivery (COD) based on specific cities in Woocommerce