Post by alindevil » Wed May 29, 2013 3:53 pm

Hello,

I've recently started to create a shipping module for my opencart powered shop but I've run into a problem.

I need correct city names to calculate shipping rates and by default that is up to the user's input skills since there's no dropdown to select the city name in the address form.

I've already created a city database with these fields : (`city_id`, `zone_id`, `country_id`, `name`, `status`)

Now i've been trying to figure out how to implement that dropdown for the cities.

I think that for country and zone the magic happens in :
\catalog\view\theme\<theme_name>\template\account\address_form.tpl
I see there's an array used for countries named $countries that gets populated like this :
$this->load->model('localisation/country');
$this->data['countries'] = $this->model_localisation_country->getCountries();

in \catalog\controller\account\address.php
Then there's the little ajax part at the end of the \catalog\view\theme\<theme_name>\template\account\address_form.tpl that feeds the zone dropdown list :

Code: Select all

<script type="text/javascript"><!--
$('select[name=\'country_id\']').bind('change', function() {
	$.ajax({
		url: 'index.php?route=account/address/country&country_id=' + this.value,
		dataType: 'json',
		beforeSend: function() {
			$('select[name=\'country_id\']').after('<span class="wait">&nbsp;<img src="catalog/view/theme/default/image/loading.gif" alt="" /></span>');
		},		
		complete: function() {
			$('.wait').remove();
		},			
		success: function(json) {
			if (json['postcode_required'] == '1') {
				$('#postcode-required').show();
			} else {
				$('#postcode-required').hide();
			}
			
			html = '<option value=""><?php echo $text_select; ?></option>';
			
			if (json['zone'] != '') {
				for (i = 0; i < json['zone'].length; i++) {
        			html += '<option value="' + json['zone'][i]['zone_id'] + '"';
	    			
					if (json['zone'][i]['zone_id'] == '<?php echo $zone_id; ?>') {
	      				html += ' selected="selected"';
	    			}
	
	    			html += '>' + json['zone'][i]['name'] + '</option>';
				}
			} else {
				html += '<option value="0" selected="selected"><?php echo $text_none; ?></option>';
			}
			
			$('select[name=\'zone_id\']').html(html);
		},
		error: function(xhr, ajaxOptions, thrownError) {
			alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
});

$('select[name=\'country_id\']').trigger('change');
//--></script> 
I managed to feed my cities with and array just like the $countries array but don't know how that ajax URL works. How can i change it to feed me the list of city names from my $cities array ?

Newbie

Posts

Joined
Wed May 29, 2013 3:03 pm

Post by sherrymardan » Fri Jul 24, 2020 8:11 pm

Hello,

If you have the solution now, please let me know... I also want the same city dropdown..

Newbie

Posts

Joined
Tue Mar 20, 2018 1:22 am
Who is online

Users browsing this forum: No registered users and 23 guests