1

In my wordpress website i m showing price of product using custom fields, and all 3 have different currency's.

And i want to detect user with IP and then print which custom field that have local currency of that country..

UK IP = price1

US IP = price2

Others IP = price3

How to print a custom field after detecting user country with IP???

<?php

if($user_country_code=="UK") {
echo get_post_meta( get_the_ID(), 'price1', true ); 
} 

else if ($user_country_code=="US"){
echo get_post_meta( get_the_ID(), 'price2', true );
}

else {
echo get_post_meta( get_the_ID(), 'price3', true ); 
}
?>
  • Do you use a specific ecommerce platform? or pure wordpress? – Ahmed Fouad Sep 25 '16 at 0:15
  • pure wordpress,,,,, – FRQ6692 Sep 25 '16 at 0:33
  • 1
    WordPress does not have a geolocation API built in, so you'd need to incorporate one, and then conditionally display the prices based on the location returned by the API. – Dave Romsey Sep 25 '16 at 0:49
2

Here is a simple approach to achieve what you are trying.

Put these functions in your theme's functions.php or plugin file.

This will let you capture 2-letter country code with API request. You have to test it remotely (if you test on localhost you'll get no country code as your IP reads as 127.0.0.1, so you have to test with real IP )

Functions

// This function will retrieve the current user IP
// needed to query the geo country
function _wp_get_ip() {
    $ip = '127.0.0.1';

    if ( ! empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
        //check ip from share internet
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif ( ! empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
        //to check ip is pass from proxy
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } elseif( ! empty( $_SERVER['REMOTE_ADDR'] ) ) {
        $ip = $_SERVER['REMOTE_ADDR'];
    }

    $ip_array = explode( ',', $ip );
    $ip_array = array_map( 'trim', $ip_array );

    if ( $ip_array[0] == '::1' ) {
        $ip_array[0] = '127.0.0.1';
    }

    return $ip_array[0];
}

// get the country code XX 
// will return null/empty if any error
function _wp_get_country_code() {

    $response = wp_remote_get( 'http://ipinfo.io/' . _wp_get_ip() . '/country' );
    if ( strlen( $country_code = (string) trim( $response['body'] ) ) == 2 ) {
        return $country_code;   
    }

    return '';
}

Conditional Price

Now you can run your conditional price output as following:

$country_code = _wp_get_country_code();

if ( $country_code == 'US' ) {

} else if ( $country_code == 'UK' ) {

} else {

    // etc...

}

Please note

This can be improved furthermore and be more efficient if you implement caching. But this is somewhat advanced for what is being asked here. I am just letting you that's not the best way to do it if you have a lot of users.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.