Jump to content

Create Separate Country and State dropdown


RasdyCute

Recommended Posts

Not sure if I understand your question.

Normally either in the controller you add the list of countries to a smarty variable, or if you have a module , you can do this in the function:

 

public function getContent()
 
Where you can get all countries and all states. Then you need some javascript in the tpl file to filter the states according to the country chosen.
 
I think it's all in the controllers/front/AddressController and the themes/<your theme folder>/Address.tpl files, as well as the javascript file themes/<your theme folder/js/tools/statesManagement.js',
 
 
In AddressController, get the countries, and assign to smarty variable:
protected function assignCountries()
{
  $this->id_country = (int)Tools::getCountry($this->_address);
  // Generate countries list
  if (Configuration::get('PS_RESTRICT_DELIVERED_COUNTRIES'))
    $countries = Carrier::getDeliveredCountries($this->context->language->id, true, true);
  else
    $countries = Country::getCountries($this->context->language->id, true);
 
  // @todo use helper
  $list = '';
  foreach ($countries as $country)
  {
    $selected = ((int)$country['id_country'] === $this->id_country) ? ' selected="selected"' : '';
    $list .= '<option value="'.(int)$country['id_country'].'"'.$selected.'>'.htmlentities($country['name'], ENT_COMPAT, 'UTF-8').'</option>';
  }
 
  // Assign vars
  $this->context->smarty->assign(array(
      'countries_list' => $list,
      'countries' => $countries,
      'sl_country' => (int)$this->id_country,
  ));
}
 

 

And in the function:

    protected function processSubmitAddress()

They check if the country has states, and if so, if a state was chosen etc. Have a look in the file for this code how they do it.
 

 

 

In the Address.tpl file,

they use the assigned variables:

 

{if $field_name eq 'Country:name' || $field_name eq 'country'}
  <div class="required form-group">
    <label for="id_country">{l s='Country'} <sup>*</sup></label>
    <select id="id_country" class="form-control" name="id_country">{$countries_list}</select>
  </div>
{/if}
 
 
Same for state:
 
 
{if $field_name eq 'State:name'}
  {assign var="stateExist" value=true}
  <div class="required id_state form-group">
    <label for="id_state">{l s='State'} <sup>*</sup></label>
    <select name="id_state" id="id_state" class="form-control">
      <option value="">-</option>
    </select>
  </div>
{/if}
 
 
The javascript glues the country an state lists together (i.e. make them dependent)
 
 
If this is not what you needed, please explain one more time more clearly what you need.
 
pascal
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...