How to Change Country Currency and Price in Country Wise

In this blog, we will discuss how to change country currency and price country-wise. Price Based on Country for WooCommerce plugin allows us to sell the same product in multiple currencies based on the country of the customer. But, we create a custom code. So, let's start.

Below are the steps to create a custom code for currency symbol and country name in WordPress:

Functionality writes on functions.php in our theme. This is a good option for certain changes.


function getVisIpAddr() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
return $_SERVER['HTTP_CLIENT_IP'];
}
else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
return $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else {
return $_SERVER['REMOTE_ADDR'];
}
}
$ip = getVisIPAddr();
$ipdat = @json_decode(file_get_contents_curl(
"http://www.geoplugin.net/json.gp?ip=".$ip));
function file_get_contents_curl($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
//Important Section
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
//Important Section
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
if($ipdat->geoplugin_countryName=='India'){
$currency_symbol='Rs ';
$country_name ='India';
add_filter('woocommerce_product_get_price', 'woocommerce_product_get_price', 10, 2 );
function woocommerce_product_get_price( $price, $product ) {
if ( get_post_type( $product->get_id() ) === 'product' )
$price = get_post_meta( $product->get_id(), "indian_price", true );
return $price;
}
}
else{
$currency_symbol='$ ';
$country_name ='Other';
add_filter('woocommerce_product_get_price', 'woocommerce_product_get_price', 10, 2 );
function woocommerce_product_get_price( $price, $product ) {
if ( get_post_type( $product->get_id() ) === 'product' )
$price = get_post_meta( $product->get_id(), "abroad_price", true );
return $price;
}
}

define( currency_symbol, $currency_symbol);
define( country_name, $country_name);

We create a custom field and get value. So, here is the below code.


get_post_meta( get_the_ID(), 'indian_price', true);

Now we can get the value for different currencies for different countries in Woocommerce development. If you have any country currency problem while developing, this is the right blog. Hope you have got what you have wanted. Please share your views in the comment section.

Comments

We Serve clients globally in diverse industries

Stay Upto Date With Our Newsletter.