Jump to content


Corporate Sponsors


Latest News: (loading..)

- - - - -

Setting default country in countries drop down


  • You cannot reply to this topic
36 replies to this topic

#21 Silverado05

  • Community Member
  • 1,605 posts
  • Real Name:Nick

Posted 18 June 2007, 00:11

Hello, How would one go about deleting the drop down box for the country. I know how to delete it and hard code just text that says United States but that wouldn't input the country else where. So how can I remove the drop down list and just use have it list one default country?

-Thanks
Search the forum and contributions before posting. If that doesn't work, keep looking, then post. The forum is for seeking help and advice NOT for someone to do your work for you. Try to do something on your on, if you are going to run a shop then learn how it works.

#22 Silverado05

  • Community Member
  • 1,605 posts
  • Real Name:Nick

Posted 27 June 2007, 15:38

What I would like to do is just remove the drop down box for the country cause I am only going to ship and sale to the United States. I would like for it to just say United States as the default country without the drop down box. I know how to remove the box and I could hard code the text United States into the form but it wouldn't carry over United States through out the site so it will post to invoices, address ect. If anyone knows how this can be achieved that would be great.

-Thanks
Search the forum and contributions before posting. If that doesn't work, keep looking, then post. The forum is for seeking help and advice NOT for someone to do your work for you. Try to do something on your on, if you are going to run a shop then learn how it works.

#23 izack1981

  • Community Member
  • 75 posts
  • Real Name:Faiz
  • Gender:Male
  • Location:Smallville

Posted 06 July 2008, 18:27

View Postefficiondave, on Apr 8 2004, 07:40 AM, said:

In addition, you'll also have edit /includes/modules/address_book_details.php to change the following line:

<td class="main"><?php echo tep_get_country_list('country', $entry['entry_country_id']) . '&nbsp;' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>

to:

<td class="main"><?php echo (($entry['entry_country_id'] == '') ? tep_get_country_list('country', 223) : tep_get_country_list('country', $entry['entry_country_id'])) . '&nbsp;' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>

so, this will make the address book to display selected country.. right?
"I am not a Newbie but I am not an Expert either..."
"Lets learn about osCommerce together!..."

#24 scottos

  • Community Member
  • 1 posts
  • Real Name:scottos

Posted 22 July 2008, 03:45

Defaulting the country (site-wide) on a per-language basis:

  • Check countries table in db to find the id for your country (or just view the source of the dropdown). In my below example, I'm using 13 for Australia.
  • Edit includes/languages/english.php, add the following anywhere you like:
    define('DEFAULT_COUNTRY_ID', 13); // 13 = Australia
    If you were using multiple languages, you'd set the country for each one.
  • includes/functions/html_output.php:
    function tep_get_country_list($name, $selected = DEFAULT_COUNTRY_ID, $parameters = '') {

Done. Using $selected = DEFAULT_COUNTRY_ID in the function arguments is a PHP language feature which defaults the variable to DEFAULT_COUNTRY_ID if none is already set.

Concise and simple. Enjoy.

Edited by scottos, 22 July 2008, 03:47.


#25 quiznosnow

  • Community Member
  • 5 posts
  • Real Name:Sabreena

Posted 14 August 2009, 01:34

View PostSilverado05, on Jun 18 2007, 12:11 AM, said:

Hello, How would one go about deleting the drop down box for the country. I know how to delete it and hard code just text that says United States but that wouldn't input the country else where. So how can I remove the drop down list and just use have it list one default country?

-Thanks

I will really appreciate if you can give me step by step instructions of how to delete the drop down box for the country and hard code text. If possible let me know how to delete the text boxes for city and state also and hard code it to display a specific country/city/state.

Thanx in advance

#26 ecartz

  • Community Member
  • 1,919 posts
  • Real Name:Matt
  • Gender:Male

Posted 15 August 2009, 04:43

View Postquiznosnow, on Aug 13 2009, 09:34 PM, said:

I will really appreciate if you can give me step by step instructions of how to delete the drop down box for the country and hard code text. If possible let me know how to delete the text boxes for city and state also and hard code it to display a specific country/city/state.
The code that displays the text boxes and drop down is
			  <tr>
				<td class="main"><?php echo ENTRY_CITY; ?></td>
				<td class="main"><?php echo tep_draw_input_field('city') . '&nbsp;' . (tep_not_null(ENTRY_CITY_TEXT) ? '<span class="inputRequirement">' . ENTRY_CITY_TEXT . '</span>': ''); ?></td>
			  </tr>
<?php
  if (ACCOUNT_STATE == 'true') {
?>
			  <tr>
				<td class="main"><?php echo ENTRY_STATE; ?></td>
				<td class="main">
<?php
	if ($process == true) {
	  if ($entry_state_has_zones == true) {
		$zones_array = array();
		$zones_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' order by zone_name");
		while ($zones_values = tep_db_fetch_array($zones_query)) {
		  $zones_array[] = array('id' => $zones_values['zone_name'], 'text' => $zones_values['zone_name']);
		}
		echo tep_draw_pull_down_menu('state', $zones_array);
	  } else {
		echo tep_draw_input_field('state');
	  }
	} else {
	  echo tep_draw_input_field('state');
	}

	if (tep_not_null(ENTRY_STATE_TEXT)) echo '&nbsp;<span class="inputRequirement">' . ENTRY_STATE_TEXT;
?>
				</td>
			  </tr>
<?php
  }
?>
			  <tr>
				<td class="main"><?php echo ENTRY_COUNTRY; ?></td>
				<td class="main"><?php echo tep_get_country_list('country') . '&nbsp;' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>
			  </tr>
If all that you want is to replace this with hard coded inputs, it would look like
			  <tr>
				<td class="main"><?php echo ENTRY_CITY; ?></td>
				<td class="main"><?php echo tep_draw_hidden_field('city', 'Toronto') . 'Toronto' . '&nbsp;' . (tep_not_null(ENTRY_CITY_TEXT) ? '<span class="inputRequirement">' . ENTRY_CITY_TEXT . '</span>': ''); ?></td>
			  </tr>
<?php
  if (ACCOUNT_STATE == 'true') {
?>
			  <tr>
				<td class="main"><?php echo ENTRY_STATE; ?></td>
				<td class="main">
<?php
	echo tep_draw_hidden_field('state', 'Ontario') . 'Ontario';

	if (tep_not_null(ENTRY_STATE_TEXT)) echo '&nbsp;<span class="inputRequirement">' . ENTRY_STATE_TEXT;
?>
				</td>
			  </tr>
<?php
  }
?>
			  <tr>
				<td class="main"><?php echo ENTRY_COUNTRY; ?></td>
				<td class="main"><?php echo tep_draw_hidden_field('country', '38') . 'Canada' . '&nbsp;' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>
			  </tr>
The '38' is the code for Canada. I got that by looking at the HTML for the select box:
<option value="38">Canada</option>
The United States is 223 and the UK is 222. You could also get these values by looking in the countries table in the database.
Always backup before making changes.

#27 quiznosnow

  • Community Member
  • 5 posts
  • Real Name:Sabreena

Posted 18 August 2009, 23:59

Thanx loads ecartz!! It's the first time I got a questions answered in this forum..

btw How do you know that I want the city/state/country to be toronto/ontario/canada?? I'm curious..:D

Thnx again
tc

#28 ecartz

  • Community Member
  • 1,919 posts
  • Real Name:Matt
  • Gender:Male

Posted 19 August 2009, 13:29

View Postquiznosnow, on Aug 18 2009, 07:59 PM, said:

btw How do you know that I want the city/state/country to be toronto/ontario/canada?? I'm curious..:D
From this post.
Always backup before making changes.

#29 DasMax

  • Community Member
  • 34 posts
  • Real Name:Jack Smith

Posted 20 August 2009, 00:24

Thanks for the tip!

#30 DasMax

  • Community Member
  • 34 posts
  • Real Name:Jack Smith

Posted 21 August 2009, 23:40

Problem is that when you select another country it doesn't remove the states drop down for the default country you selected. I believe this may be fixable with javascript, but I wouldn't know where to start.

#31 ecartz

  • Community Member
  • 1,919 posts
  • Real Name:Matt
  • Gender:Male

Posted 23 August 2009, 03:30

View PostDasMax, on Aug 21 2009, 07:40 PM, said:

Problem is that when you select another country it doesn't remove the states drop down for the default country you selected. I believe this may be fixable with javascript, but I wouldn't know where to start.
In a stock osCommerce, in includes/form_check.js.php, just before the
//--></script>
add
<?php 
  $countries_with_zones = array();
  $countries_with_zones_query = tep_db_query("SELECT c.countries_iso_code_2 FROM countries c, zones z WHERE c.countries_id = z.zone_country_id GROUP BY z.zone_country_id ORDER BY z.zone_country_id");
  while ( $country_with_zone = tep_db_fetch_array($countries_with_zones_query) ) {
	$countries_with_zones[] = $country_with_zone['countries_iso_code_2'];
  }
?>
function displayStateInputs(value) {
  var state_input = document.getElementById('state');
<?php
  foreach ($countries_with_zones as $country_with_zone_code) {
?>
  var <?php echo $country_with_zone_code; ?>_states_menu = document.getElementById('<?php echo $country_with_zone_code; ?>_states');
   <?php echo $country_with_zone_code; ?>_states_menu.style.display = "none";
<?php
  }
?>

  switch (value) {
<?php
  foreach ($countries_with_zones as $country_with_zone_code) {
?>
	case '<?php echo $country_with_zone_code; ?>':
	  <?php echo $country_with_zone_code; ?>_states_menu.style.display = "";
	  state_input.value = <?php echo $country_with_zone_code; ?>_states_menu.value;
	  state_input.style.display = "none";
	  break;
<?php
  }
?>
	default:
	  state_input.value = '';
	  state_input.style.display = "";
  }
}
function updateState(value) {
  document.getElementById('state').value = value;
}
In create_account.php (around line 18), before
  $process = false;
  if (isset($HTTP_POST_VARS['action']) && ($HTTP_POST_VARS['action'] == 'process')) {
	$process = true;
add
  $state = '';
Note that you could set this to a particular state if you want your drop down to default to a particular state.

In create_account.php, around line 263, change
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
to
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" onload="displayStateInputs('US')">
Replace US with another country's two character ISO code as appropriate or set to an empty string (two single quotes with nothing between them) to default to showing a text box rather than a select menu. This value should correspond with the value that you set for your countries drop down menu default (leave empty if you are not setting a default for your countries drop down).

Around lines 411 to 439, change
<?php
  if (ACCOUNT_STATE == 'true') {
?>
			  <tr>
				<td class="main"><?php echo ENTRY_STATE; ?></td>
				<td class="main">
<?php
	if ($process == true) {
	  if ($entry_state_has_zones == true) {
		$zones_array = array();
		$zones_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' order by zone_name");
		while ($zones_values = tep_db_fetch_array($zones_query)) {
		  $zones_array[] = array('id' => $zones_values['zone_name'], 'text' => $zones_values['zone_name']);
		}
		echo tep_draw_pull_down_menu('state', $zones_array);
	  } else {
		echo tep_draw_input_field('state');
	  }
	} else {
	  echo tep_draw_input_field('state');
	}

	if (tep_not_null(ENTRY_STATE_TEXT)) echo '&nbsp;<span class="inputRequirement">' . ENTRY_STATE_TEXT . '</span>';
?>
				</td>
			  </tr>
<?php
  }
?>
to
<?php
  if (ACCOUNT_STATE == 'true') {
?>
			  <tr>
				<td class="main"><?php echo ENTRY_STATE; ?></td>
				<td class="main">
<?php
	$zones_array = array();
	$select_zones_query = tep_db_query("SELECT c.countries_iso_code_2, z.zone_name FROM countries c, zones z WHERE c.countries_id = z.zone_country_id ORDER BY z.zone_country_id, z.zone_name");
	while ($zone_option = tep_db_fetch_array($select_zones_query)) {
	  $country_with_zone_code = $zone_option['countries_iso_code_2'];

	  if (!is_array($zones_array[$country_with_zone_code]) {
		$zones_array[$country_with_zone_code] = array(array('id' => '', 'text' => PLEASE_SELECT_A_STATE));
	  }

	  $zones_array[$country_with_zone_code][] = array('id' => $zone_option['zone_name'], 'text' => $zone_option['zone_name']);
	}
	foreach ( $zones_array as $country_with_zone_code => $country_zones_array) {
	  echo tep_draw_pull_down_menu($country_with_zone_code . '_state', $country_zones_array, $state, 'onchange="displayStateInputs(this.value);"');
	}
	echo tep_draw_input_field('state', '', 'onchange="updateState(this.value)"');

	if (tep_not_null(ENTRY_STATE_TEXT)) echo '&nbsp;<span class="inputRequirement">' . ENTRY_STATE_TEXT . '</span>';
?>
				</td>
			  </tr>
<?php
  }
?>
and in includes/languages/english/create_account.php (or whatever language), add
  define('PLEASE_SELECT_A_STATE', 'Please select a state');
Note: I haven't tested this. Backup before trying.
Always backup before making changes.

#32 DasMax

  • Community Member
  • 34 posts
  • Real Name:Jack Smith

Posted 23 August 2009, 04:27

Parse error: syntax error, unexpected '{' in /homepages/8/d274908695/htdocs/create_account.php on line 435

Line 435 - if (!is_array($zones_array[$country_with_zone_code]) {

This is the error I get.

#33 DasMax

  • Community Member
  • 34 posts
  • Real Name:Jack Smith

Posted 23 August 2009, 04:39

Doesn't osCommerece work off of database ids instead of country ISO code? For example 'US' is really '223'.

#34 ecartz

  • Community Member
  • 1,919 posts
  • Real Name:Matt
  • Gender:Male

Posted 23 August 2009, 19:10

Revised version (also fixes missing parenthesis):

In a stock osCommerce, in includes/form_check.js.php, just before the
//--></script>
add
<?php
  $countries_with_zones = array();
  $country_id_for = array();
  $countries_with_zones_query = tep_db_query("SELECT c.countries_id, c.countries_iso_code_2 FROM countries c, zones z WHERE c.countries_id = z.zone_country_id GROUP BY z.zone_country_id ORDER BY z.zone_country_id");
  while ( $country_with_zone = tep_db_fetch_array($countries_with_zones_query) ) {
	$countries_with_zones[] = $country_with_zone['countries_iso_code_2'];
	$country_id_for[$country_with_zone['countries_iso_code_2']] = $country_with_zone['countries_id'];
  }
?>
function displayStateInputs(value) {
  var state_input = document.getElementsByName('state')[0];
<?php
  foreach ($countries_with_zones as $country_with_zone_code) {
?>
  var <?php echo $country_with_zone_code; ?>_states_menu = document.getElementsByName('<?php echo $country_with_zone_code; ?>_state')[0];
  <?php echo $country_with_zone_code; ?>_states_menu.style.display = 'none';
<?php
  }
?>

  switch (value) {
<?php
  foreach ($countries_with_zones as $country_with_zone_code) {
?>
	case '<?php echo $country_id_for[$country_with_zone_code]; ?>':
	  <?php echo $country_with_zone_code; ?>_states_menu.style.display = '';
	  state_input.value = <?php echo $country_with_zone_code; ?>_states_menu.value;
	  state_input.style.display = 'none';
	  break;
<?php
  }
?>
	default:
	  state_input.value = '';
	  state_input.style.display = '';
  }
}
function updateState(value) {
  document.getElementsByName('state')[0].value = value;
}
In create_account.php (around line 18), before
  $process = false;
  if (isset($HTTP_POST_VARS['action']) && ($HTTP_POST_VARS['action'] == 'process')) {
	$process = true;
add
  $state = '';
  $country = '223';
Note that you could set $state to a particular state (e.g. $state = 'Alabama';) if you want your drop down to default to a particular state. Same thing for country. Setting it to 223 makes the US the default. Set it appropriately for your store. Setting it to an empty string would make it show the countries drop down with no default and the text input for state (what create_account.php does now).

In create_account.php, around line 263, change
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
to
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" onload="displayStateInputs('<?php echo $country; ?>')">
Around lines 411 to 443, change
<?php
  if (ACCOUNT_STATE == 'true') {
?>
			  <tr>
				<td class="main"><?php echo ENTRY_STATE; ?></td>
				<td class="main">
<?php
	if ($process == true) {
	  if ($entry_state_has_zones == true) {
		$zones_array = array();
		$zones_query = tep_db_query("select zone_name from " . TABLE_ZONES . " where zone_country_id = '" . (int)$country . "' order by zone_name");
		while ($zones_values = tep_db_fetch_array($zones_query)) {
		  $zones_array[] = array('id' => $zones_values['zone_name'], 'text' => $zones_values['zone_name']);
		}
		echo tep_draw_pull_down_menu('state', $zones_array);
	  } else {
		echo tep_draw_input_field('state');
	  }
	} else {
	  echo tep_draw_input_field('state');
	}

	if (tep_not_null(ENTRY_STATE_TEXT)) echo '&nbsp;<span class="inputRequirement">' . ENTRY_STATE_TEXT . '</span>';
?>
				</td>
			  </tr>
<?php
  }
?>
			  <tr>
				<td class="main"><?php echo ENTRY_COUNTRY; ?></td>
				<td class="main"><?php echo tep_get_country_list('country') . '&nbsp;' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>
			  </tr>
to
<?php
  if (ACCOUNT_STATE == 'true') {
?>
			  <tr>
				<td class="main"><?php echo ENTRY_STATE; ?></td>
				<td class="main">
<?php
	$zones_array = array();
	$select_zones_query = tep_db_query("SELECT c.countries_iso_code_2, z.zone_name FROM countries c, zones z WHERE c.countries_id = z.zone_country_id ORDER BY z.zone_country_id, z.zone_name");
	while ($zone_option = tep_db_fetch_array($select_zones_query)) {
	  $country_with_zone_code = $zone_option['countries_iso_code_2'];

	  if (!is_array($zones_array[$country_with_zone_code])) {
		$zones_array[$country_with_zone_code] = array(array('id' => '', 'text' => PLEASE_SELECT_A_STATE));
	  }

	  $zones_array[$country_with_zone_code][] = array('id' => $zone_option['zone_name'], 'text' => $zone_option['zone_name']);
	}
	foreach ( $zones_array as $country_with_zone_code => $country_zones_array) {
	  echo tep_draw_pull_down_menu($country_with_zone_code . '_state', $country_zones_array, $state, 'onchange="updateState(this.value)" style="display:none"');
	}
	echo tep_draw_input_field('state');

	if (tep_not_null(ENTRY_STATE_TEXT)) echo '&nbsp;<span class="inputRequirement">' . ENTRY_STATE_TEXT . '</span>';
?>
				</td>
			  </tr>
<?php
  }
?>
			  <tr>
				<td class="main"><?php echo ENTRY_COUNTRY; ?></td>
				<td class="main"><?php echo tep_get_country_list('country', $country, 'onchange="displayStateInputs(this.value)"') . '&nbsp;' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>
			  </tr>
and in includes/languages/english/create_account.php (or whatever language), add
  define('PLEASE_SELECT_A_STATE', 'Please select a state');
You also need to have the charset set appropriately for the various states you will show. UTF-8 might work, although the osCommerce javascript may then fail in a browser using Shift-JIS (Japanese encoding).

Backup before trying.

Edited by ecartz, 23 August 2009, 19:20.

Always backup before making changes.

#35 DasMax

  • Community Member
  • 34 posts
  • Real Name:Jack Smith

Posted 24 August 2009, 17:56

Tested in IE 8 and Firefox 3.5.2 - OMG it works!! :D

Are you going to put it into contribution format?

#36 yoseph

  • Community Member
  • 12 posts
  • Real Name:Yoseph Shepherd

Posted 23 May 2011, 19:18

View Postdontera, on 26 March 2004, 21:36, said:

This is something I had wrestled with for some time, until the obvious hit me..

I tried various ways to output a sorted countries list, changing the country I wanted at top to be ID 0 and whatnot, didnt work. Finally I relaized I was overlooking the obvious of using <option .... selected>

Thankfully, the requisite functions have built-in "selected" handling, thus setting your default country is easy.

Edit create_account.php and find

              <tr>
                <td class="main"><?php echo ENTRY_COUNTRY; ?></td>
                <td class="main"><?php echo tep_get_country_list('country') . '&nbsp;' . (tep_not_null(ENTRY_COUNTRY_TEXT) ? '<span class="inputRequirement">' . ENTRY_COUNTRY_TEXT . '</span>': ''); ?></td>
              </tr>

you want to change this line
php echo tep_get_country_list('country') .
to be
php echo tep_get_country_list('country','223') .
223 == the country ID for United States. YOu can set that to be any country by looking either in the database countries list for your countries ID, or even easier, look at the HTML of the create_account.php page and find where the country drop down list is, search for your country and grab the "value".

Hope this helps.

Wow! Thank you. Exactly what I needed and provided a perfect fix. Now I can move on to the next project lol

#37 Anden86

  • Community Member
  • 1 posts
  • Real Name:Andreas J

Posted 08 April 2012, 17:00

Sweden is 203 :)
you can now find the country number in the database table named countries!