Okay
  Public Ticket #1627583
Using Multiple Countries
Closed

Comments

  • rmbettencourt started the conversation

    So I want to redirect based on countries (for language purposes), however how do I do this, since it only works for one country code. I have added my list to the private php but how do I call it in jquery?


    Here is what I have:

    jQuery(document).ready(function(){
                    
        jQuery.get( 'assets/php/geo.php', function( countryMatches ) {
            if ( 'FRANCAISE' == countryMatches ) { 
                window.location.replace('fr/arrive-bientot.html');
            } else if ( 'ESPANOL' == countryMatches ) {
                window.location.replace('es/proximamente.html');
            } else if ( 'DEUTSCH' == countryMatches ) {
                window.location.replace('de/demnaechst.html');
            } else {
                window.location.replace('en/coming-soon.html');
            }
        } );
                    
    });
    

    and here is my array in the geo.php file:

    private static $FRANCAISE = array("BE", "FR", "GF", "PF");
            
    private static $ESPANOL = array("AR", "BO", "CL", "CO", "CR", "CU", "DO", "EC", "ES", "SV", "GQ", "GT", "HN", "MX", "NI", "PA", "PY", "PE", "PR", "UY", "VE");
            
    private static $DEUTSCH = array("AT", "CH", "DE", "LI");
    

    and here is in the "country match" section:

    private function countryMatches($country, $except)
    {
        $cs = $country;
                
        if( ! is_array($country) )
        {
            switch( strtoupper($country) )
            {
                 case "AMERICA":
                     $country = self::$AMERICA;
                     break;
                        
                 case "ASIA":
                     $country = self::$ASIA;
                     break;
                            
                 case "BALKANS":
                 case "BALKAN":
                     $country = self::$BALKANS;
                     break;
                        
                 case "CARIBBEAN":
                     $country = self::$CARIBBEAN;
                     break;
                        
                 case "CENTRALAMERICA":
                     $country = self::$CENTRALAMERICA;
                     break;
                        
                 case "EUROPE":
                     $country = self::$EUROPE;
                     break;
                        
                  case "MIDDLEEAST":
                  case "MIDDLE EAST":
                      $country = self::$MIDDLEEAST;
                      break;
                        
                  case "NORTHAMERICA":
                  case "UNITED STATES":
                      $country = self::$NORTHAMERICA;
                      break;
                        
                  case "SOUTHAMERICA":
                  case "LATIN AMERICA":
                      $country = self::$SOUTHAMERICA;
                      break;
                            
                  case "FRANCAISE":
                      $country = self::$FRANCAISE;
                      break;
                        
                  case "ESPANOL":
                      $country = self::$ESPANOL;
                      break;
                            
                  case "DEUTSCH":
                      $country = self::$DEUTSCH;
                      break;
                        
                  default:
                      $country = array($country);
        }
    }
    
  • [deleted] replied

    Hi,

    In that file called geo.php you can simply output the country code:

    echo CountryDetector::getCountryCode();

    Then you can process the country codes in JS:

    jQuery(document).ready(function(){
                    
        jQuery.get( 'assets/php/geo.php', function( countryCode ) {
            if ( $.inArray( countryCode, [ "BE", "FR", "GF", "PF" ] ) != -1 ) { 
                window.location.replace('fr/arrive-bientot.html');
            } else if ( 'ES' == countryCode ) {
                window.location.replace('es/proximamente.html');
            } else if ( 'DE' == countryCode ) {
                window.location.replace('de/demnaechst.html');
            } else {
                window.location.replace('en/coming-soon.html');
            }
        } );
                    
    });

    I've included the following code:

    $.inArray( countryCode, [ "BE", "FR", "GF", "PF" ] ) != -1

    just to show that you can match multiple country codes to match single landing page. 

    I hope this makes sense for you.

  • rmbettencourt replied

    It still is not working. It all just defaults to to en.

  • [deleted] replied

    Where do you have this file "assets/php/geo.php" hosted? Can you give me the URL, or probably  give me the FTP of your site so I can check what it returns and the JS file to edit.

    To include your credentials click Insert Credentials button in editor toolbar. (See how)

  •   rmbettencourt replied privately
  • [deleted] replied

    Hi rmbettencourt,

    Thanks for providing credentials, can you please tell me the domain name because I cannot access your site?!

  •   rmbettencourt replied privately
  • [deleted] replied

    Hi,

    The page assets/php/geo.php was showing PHP errors:


    (view large version)

    So I have created a new file for you, and renamed that geo.php to geo-old.php

    So now the new geo.php file returns properly the GEO code:

    http://rosalinebettencourt.com/assets/php/geo.php

    This should work for you.

  • rmbettencourt replied

    Great! So is the code on index.html correct? The main index.html file in the root folder. 


    Here is it as well:


    jQuery.get( 'assets/php/geo.php', function( countryCode ) {
        if ( $.inArray( countryCode, [ "BE", "FR", "GF", "PF" ] ) != -1 ) { 
            window.location.replace('fr/arrive-bientot.html');
        } else if ( $.inArray( countryCode, [ "AR", "BO", "CL", "CO", "CR", "CU", "DO", "EC", "ES", "SV", "GQ", "GT", "HN", "MX", "NI", "PA", "PY", "PE", "PR", "UY", "VE" ] ) != -1 ) {
            window.location.replace('es/proximamente.html');
        } else if ( $.inArray( countryCode, [ "AT", "CH", "DE", "LI" ] ) != -1 ) {
            window.location.replace('de/demnaechst.html');
        } else {
            window.location.replace('en/coming-soon.html');
        }
    } );
  • [deleted] replied

    Hi,

    Yes thats the correct code, you can manage the list of countries in the array for specific country code with redirection.

    Now it seems to work just fine!