Display dynamic content based on visitor's location using Smart IP

One practical use of Drupal contributed Smart IP module is to display different content (like different price and links) based on your visitor's location.

The code snippet are as follows:

For Drupal 6 and 7

  
<?php $smart_ip_session = smart_ip_session_get('smart_ip'); ?>
<?php if ($smart_ip_session['location']['country_code'] == 'AR'): ?>
  <p>Argentina HTML content specific</p>
<?php elseif ($smart_ip_session['location']['country_code'] == 'UY'): ?>
  <p>Uruguay HTML content specific</p>
<?php else: ?>
  <p>Fallback default content.</p>
<?php endif; ?>
  

For Drupal 8

  
<?php $location = \Drupal::service('smart_ip.smart_ip_location'); ?>
<?php if ($location->get('countryCode') == 'AR'): ?>
  <p>Argentina HTML content specific</p>
<?php elseif ($location->get('countryCode') == 'UY'): ?>
  <p>Uruguay HTML content specific</p>
<?php else: ?>
  <p>Fallback default content.</p>
<?php endif; ?>
  

Note: The $smart_ip_session['location']['country_code'] and $location->get('countryCode') are the ISO_3166-1_alpha-2 which can be found the list of country codes at http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2.

Comments

It depends on where you want to show this and consider that you should have access to execute PHP codes in that page. The quick way is to put in the block, enabling the PHP code execution. Or in your theme template.

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.