Auto-select country dropdown based on visitor’s location

The module can automatically select the value of country dropdown for visitors based on their location. For example if a visitor is registering on your WHMCS from Italy, the module automatically selects Italy in the dropdown.

If you want to enable this feature you have to edit 2 template files of your WHMCS.

Warning

Please notice that in order to complete these steps, a basic knowledge of Smarty is required and that some templates, especially the older ones, are not supported.

First of all open your {YOUR_TEMPLATE}/clientregister.tpl and look for this section of code:

Find Country dropdown

<select id="country" name="country" class="form-control">
    {foreach $clientcountries as $countryCode => $countryName}
        <option value="{$countryCode}"{if (!$clientcountry && $countryCode eq $defaultCountry) || ($countryCode eq $clientcountry)} selected="selected"{/if}>
            {$countryName}
        </option>
    {/foreach}
</select>

Keep in mind that the code could be a bit different. You simply have to change if statement above highlighted in red like follows (changes are highlighted in blue).

Copy the following If statement

{if (!$clientcountry && !$al_country && $countryCode eq $defaultCountry) || ($countryCode eq $clientcountry) || (!$clientcountry AND $countryCode eq $al_country)}

The result should look like follows:

Result

<select id="country" name="country" class="form-control">
    {foreach $clientcountries as $countryCode => $countryName}
        <option value="{$countryCode}"{if (!$clientcountry && !$al_country && $countryCode eq $defaultCountry) || ($countryCode eq $clientcountry) || (!$clientcountry AND $countryCode eq $al_country)}>
            {$countryName}
        </option>
    {/foreach}
</select>

Now open your {YOUR_ORDERFORM}/viewcart.tpl and look for this section of code:

Find Country dropdown

<select id="country" name="country" class="form-control">
    {foreach from=$countries key=thisCountryCode item=thisCountryName}
        <option value="{$thisCountryCode}" {if $thisCountryCode eq $clientsdetails.country}selected="selected"{/if}>{$thisCountryName}</option>
    {/foreach}
</select>

Keep in mind that also in this case the code could be a bit different. You have to replace the if statement highlighted above with the following one:

Copy the following If statement

{if $thisCountryCode eq $al_country} selected="selected"{/if}

The result should look like follows:

Result

<select id="country" name="country" class="form-control">
    {foreach from=$countries key=thisCountryCode item=thisCountryName}
        <option value="{$thisCountryCode}"{if $thisCountryCode eq $al_country} selected="selected"{/if}>{$thisCountryName}</option>
    {/foreach}
</select>

On some templates the select that contains the list of countries is not generated in a foreach loop. The list could be incorporated in a built-in variable like {$clientcountriesdropdown}. In this case simply add replace modifier as follows:

Source Code Title

{$clientcountriesdropdown|replace:'selected="selected"':''|replace:"value=\"$al_country\"":"value=\"$al_country\" selected=\"selected\""}

Posted : - Wed, Apr 6, 2016 3:58 PM. This article has been viewed 1825 times.
Online URL: http://docs.katamaze.com/article/auto-select-country-dropdown-based-on-visitor-s-location-16.html

Powered by PHPKB (Knowledge Base Software)