• Hi there,

    I’m using this plugin with Maxmind Precision Web API and the Open Exchange Rates API for basic price localization on my site. The locations I need to test for are as follows:

    If user country = UK, display the price field (This is stored in an ACF number field)
    If user continent is NA, use open Exchange API to convert to USD
    If user is continent EU but not UK, use exchange API to convert to EU.

    At present I’m using the following code whenever there’s a product listing on a page (usually around 10 products per page):

    <?php
    	if (function_exists('geoip_detect2_get_info_from_current_ip')) {
    		$userInfo = geoip_detect2_get_info_from_current_ip();
    
    		if ($userInfo->country->isoCode =='GB')  {
    
    			get_template_part('templates/get-price-gbp');
    			
    		} else {
          // Query IP and return price in either EU or USD
    			get_template_part('templates/get-price-conversion');	
    		}
    
    	} else {
    		// If service unavailabe display GBP
    		get_template_part('templates/get-price-gbp');
    
    	}
    ?>

    If the country code is not GB I load the following template which uses the open exchange rate API with get_conversion:

    <?php 
    
      $price = get_field("price");
    
      if( $price ) {
    
    		if (function_exists('geoip_detect2_get_info_from_current_ip')) {
    			$userInfo = geoip_detect2_get_info_from_current_ip();
    
    				if ($userInfo->continent->code == 'EU' && $userInfo->country->isoCode !=='GB' )  {
    				echo '€';	
    				echo get_conversion( 'number='. $price . '&from=gbp&to=eur' );
    
    			} elseif ($userInfo->continent->code == 'NA') {
    
    				echo '$';	
    				echo get_conversion( 'number='. $price . '&from=gbp&to=usd' );
    
    			} else {
    				// If user is anywhere else default to GBP
    				echo '£';
    				echo $price;
    
    			}
    
    		} 
    
    	} else {
    
    			echo 'Price TBC';
    			
    		}
    ?>
    

    The problem I’m having is that sometimes USD are being displayed on pages where the user IP is in the UK, note: this tends to correct itself when the user navigates to other pages but I’d still like to understand why this is happenning and how to fix it.

    I’m also certain there’s a much more elegant way of using the API but my PHP isn’t great. If anyone has any better suggestions as to how they would implement this that would be really great. Many thanks!

    I’m using the latest version 3.0.1 and latest wordpress 5.3.2, php version 7.3.13. No reverse proxies being used

  • The topic ‘Help Troubleshooting Country Code Errors’ is closed to new replies.