bidou88
bidou88

Reputation: 694

Woocommerce - Change currency depending on billing country

I'm trying to change the currency depending on the customer's billing country. Is it possible to do that maybe with an override of the "tax calculation" function as it is updated when the customer change the country ?

At the moment, I have this function (function.php) :

add_filter('wcml_client_currency','change_currency');

function change_currency($client_currency){
   global $woocommerce;
   $country = $woocommerce->customer->get_country();
   if($country == 'CH') {
       $client_currency = 'CHF'; //currency code
   } else {
       $client_currency = 'EUR'; //currency code
   }
   return $client_currency;
}

Thanks for your help.

Upvotes: 2

Views: 1997

Answers (1)

You can use this following code:

add_filter('woocommerce_get_price', 'return_custom_price', $product, 2);

function return_custom_price($price, $product) {    
global $post, $woocommerce;
$tot_qty = $woocommerce->cart->cart_contents_count;
// Array containing country codes
$county = array('NZ', 'AU', 'GB', 'IE');
// Amount to increase by
//$amount = 10;
change_existing_currency_symbol('NZD$', 'NZD');

if($woocommerce->customer->get_shipping_country() == 'NZ'){
    $amount = 0.00;
    $amount = 26.19;
}
else if($woocommerce->customer->get_shipping_country() == 'GB'){
    $amount = 0.00;
    $amount = 19.04;
}
else if($woocommerce->customer->get_shipping_country() == 'IE'){
    $amount = 0.00;
    $amount = 19.04;
}
else {
    $amount = 0.00;
    $amount = 28.19;
}

//echo $amount;
// If the custromers shipping country is in the array
if ( in_array( $woocommerce->customer->get_shipping_country(), $county ) ){
    // Return the price plus the $amount
   return $new_price = $amount;
   die();
} else {
    // Otherwise just return the normal price
    return $price;
    die();
}
}

add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);

function change_existing_currency_symbol( $currency_symbol, $currency ) {
    global $post, $woocommerce;
    $my_country = $woocommerce->customer->get_shipping_country();
     switch( $my_country ) {
          case 'GB': $currency_symbol = '£'; 
          break;

          case 'NZ': $currency_symbol = '$'; 
          break;

          case 'IE': $currency_symbol = '€'; 
          break;

          default:
                    $currency_symbol = '$'; 

     }
     return $currency_symbol;
}

Upvotes: 1

Related Questions