Jump to content

 

stasok

Member Since 12 Mar 2012
Offline Last Active Apr 20 2018 12:09 PM
-----

Posts I've Made

In Topic: Drop Down Select City

15 April 2018 - 03:02 PM

That's very interesting.

I did some experiments according to tbirnseth's usefull thoughts.

The target is to alter city input field to select box dependent to state selection.

But does the locations' functionality about cities wildcards remain the same ?? Because the final target is, on city selection to calculate the shipping cost of an order at checkout step,

So I created a table like this
 

DROP TABLE IF EXISTS `katoikia`.`cscart_cities`;
CREATE TABLE  `katoikia`.`cscart_cities` (
  `city_id` mediumint(<img src='http://forum.cs-cart.com/public/style_emoticons/<#EMO_DIR#>/icon_cool.gif' class='bbc_emoticon' alt='8)' /> unsigned NOT NULL auto_increment,
  `country_code` varchar(2) NOT NULL default '',
  `code` varchar(32) NOT NULL default '',
  `state_id` varchar(32) NOT NULL,
  `status` char(1) NOT NULL default 'A',
  PRIMARY KEY  (`city_id`),
  UNIQUE KEY `cs` (`country_code`,`code`),
  KEY `code` (`code`),
  KEY `country_code` (`country_code`)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
then I added functions for bulding and seeking cities, almost like every state function at core/fn.locations.php like that:

//
// Get countries list
//
function fn_get_countries($lang_code = CART_LANGUAGE, $only_avail = false, $paginate = false)
{
$only_avail = ($only_avail == true) ? " WHERE a.status = 'A'" : '';
$limit = '';
$page = empty($_REQUEST['page']) ? 1 : $_REQUEST['page'];
if ($paginate == true) {
  $country_count = db_get_field("SELECT count(*) FROM ?:countries");
  $limit = fn_paginate($page, $country_count, Registry::get('settings.Appearance.admin_elements_per_page'));
}
return db_get_array("SELECT a.code, a.code_A3, a.code_N3, a.status, a.region, b.country FROM ?:countries as a LEFT JOIN ?:country_descriptions as b ON b.code = a.code AND b.lang_code = ?s $only_avail ORDER BY b.country $limit", $lang_code);
}
//
// Get countries simple list
//
function fn_get_simple_countries($avail_only = false, $lang_code = CART_LANGUAGE)
{
$avail_cond = ($avail_only == true)  ? "WHERE a.status = 'A'" : '';
return db_get_hash_single_array("SELECT a.code, b.country FROM ?:countries as a LEFT JOIN ?:country_descriptions as b ON b.code = a.code AND b.lang_code = ?s $avail_cond ORDER BY b.country", array('code', 'country'), $lang_code);
}
//
// Get states list for specific country
//
function fn_get_states($country = '', $lang_code = CART_LANGUAGE, $paginate = false)
{
if (empty($country)) {
  return '';
}
$limit = '';
if ($paginate == true) {
  $state_count = db_get_field("SELECT COUNT(*) FROM ?:states WHERE country_code = ?s", $country);
  $limit = fn_paginate(@$_REQUEST['page'], $state_count, Registry::get('settings.Appearance.admin_elements_per_page'));
}
return db_get_array("SELECT a.state_id, a.country_code, a.code, a.status, b.state FROM ?:states as a LEFT JOIN ?:state_descriptions as b ON b.state_id = a.state_id AND b.lang_code = ?s WHERE a.country_code = ?s ORDER BY b.state $limit", $lang_code, $country);
}
//
// Get cities list for specific state
//
function fn_get_cities($state = '', $lang_code = CART_LANGUAGE, $paginate = false)
{
if (empty($state)) {
  return '';
}
$limit = '';
if ($paginate == true) {
  $city_count = db_get_field("SELECT COUNT(*) FROM ?:cities WHERE state_id = ?s", $state);
  $limit = fn_paginate(@$_REQUEST['page'], $city_count, Registry::get('settings.Appearance.admin_elements_per_page'));
}
return db_get_array("SELECT a.city_id, a.country_code, a.code, a.state_id, a.status
FROM ?:cities as a WHERE a.country_code = ?s AND a.state_id = ?s ORDER BY a.code $limit", $country, $state);
}

//
// Get states key-value list
//
function fn_get_simple_states($country = '', $lang_code = CART_LANGUAGE, $paginate = false)
{
if (empty($country)) {
  $country = 'US'; //!!! FIXME: what is the logic should be?
}
return db_get_hash_single_array("SELECT a.code, b.state FROM ?:states as a LEFT JOIN ?:state_descriptions as b ON b.state_id = a.state_id AND b.lang_code = ?s WHERE a.country_code = ?s ORDER BY b.state", array('code', 'state'), $lang_code, $country);
}
//
// Get cities key-value list
//
function fn_get_simple_cities($state = '', $lang_code = CART_LANGUAGE, $paginate = false)
{
if (empty($state)) {
  $state = ''; //!!! FIXME: what is the logic should be?
}
return db_get_hash_single_array("SELECT a.city_id, a.code FROM ?:cities as a WHERE a.state_id = ?s ORDER BY a.code", array('city_id', 'code'), $state);
}
//
// Get states list for specific country
//
function fn_get_all_states($lang_code = CART_LANGUAGE, $extended = false, $avail_only = true)
{
$avail_cond = ($avail_only == true) ? " WHERE a.status = 'A' " : '';
if ($extended == true) {
  return db_get_array("SELECT a.state_id, a.code, b.state, c.country FROM ?:states as a LEFT JOIN ?:state_descriptions as b ON b.state_id = a.state_id AND b.lang_code = ?s LEFT JOIN ?:country_descriptions as c ON c.code = a.country_code AND c.lang_code = ?s $avail_cond ORDER BY a.country_code, b.state", $lang_code, $lang_code);
} else {
  return db_get_hash_multi_array("SELECT a.country_code, a.code, b.state FROM ?:states as a LEFT JOIN ?:state_descriptions as b ON b.state_id = a.state_id AND b.lang_code = ?s $avail_cond ORDER BY a.country_code, b.state", array('country_code'), $lang_code);
}
}
//
// Get cities list for specific state
//
function fn_get_all_cities($lang_code = CART_LANGUAGE, $extended = false, $avail_only = true)
{
$avail_cond = ($avail_only == true) ? " WHERE a.status = 'A' " : '';
if ($extended == true) {
  return db_get_array("SELECT a.city_id, a.code, a.state_id, b.state, c.country FROM ?:cities as a LEFT JOIN ?:state_descriptions as b ON b.state_id = a.state_id AND b.lang_code = ?s LEFT JOIN ?:country_descriptions as c ON c.code = a.country_code AND c.lang_code = ?s ORDER BY a.country_code, b.state", $lang_code, $lang_code);
} else {
  return db_get_hash_multi_array("SELECT a.country_code, a.state_id, a.city_id, a.code FROM ?:cities as a ORDER BY a.country_code, a.code", array('state_id'), $lang_code);
}
}

// Get state name (results are cached)
function fn_get_state_name($state_code, $country_code, $lang_code = CART_LANGUAGE)
{
static $states = array();
if (!isset($states[$country_code][$state_code])) {
  $states[$country_code][$state_code] = db_get_field("SELECT ?:state_descriptions.state FROM ?:states LEFT JOIN ?:state_descriptions ON ?:state_descriptions.state_id = ?:states.state_id AND ?:state_descriptions.lang_code = ?s WHERE ?:states.country_code = ?s AND ?:states.code = ?s", $lang_code, $country_code, $state_code);
}
return $states[$country_code][$state_code];
}
// Get city name (results are cached)
function fn_get_city_name($state_id, $country_code)
{
static $cities = array();
if (!isset($cities[$country_code][$state_id])) {
  $cities[$country_code][$state_id] = db_get_field("SELECT ?:cities.code FROM ?:cities WHERE ?:cities.country_code = ?s AND ?:cities.state_id = ?s", $country_code, $state_id);
}
return $cities[$country_code][$state_id];
}

// Get country name (results are cached)
function fn_get_country_name($country_code, $lang_code = CART_LANGUAGE)
{
static $countries = array();
if (empty($countries[$country_code])) {
  $countries[$country_code] = db_get_field("SELECT country FROM ?:country_descriptions WHERE code = ?s AND lang_code = ?s", $country_code, $lang_code);
}

return $countries[$country_code];
}
// Get countries name (results are cached)
function fn_get_countries_name($country_codes, $lang_code = CART_LANGUAGE)
{
$countries = db_get_hash_array("SELECT country, code FROM ?:country_descriptions WHERE code IN (?a) AND lang_code = ?s", 'code', $country_codes, $lang_code);
return $countries;
}
//
// Get all destinations list
//
function fn_get_destinations($lang_code = CART_LANGUAGE)
{
$destinations = db_get_hash_array("SELECT a.destination_id, a.status, a.localization, b.destination FROM ?:destinations as a LEFT JOIN ?:destination_descriptions as b ON a.destination_id = b.destination_id AND b.lang_code = ?s ORDER BY destination", 'destination_id', $lang_code);
$default = $destinations[1];
unset($destinations[1]);
array_unshift($destinations, $default);
return $destinations;
}
//
// Get destination name
//
function fn_get_destination_name($destination_id, $lang_code = CART_LANGUAGE)
{
if (!empty($destination_id)) {
  return db_get_field("SELECT destination FROM ?:destination_descriptions WHERE destination_id = ?i AND lang_code = ?s", $destination_id, $lang_code);
}
return false;
}
//
// Helper for fn_get_available_destination function
//
// @$partial - check for partial equality
//
function fn_check_element($elms, $elm, $partial = false)
{
if (empty($elm)) {
  return false;
}
$suitable = false;
foreach ($elms as $k => $v) {
  if ($partial == true) {
   $__tmp = str_replace(array('*', '?', '/'), array('.*', '.', '\/'), $v);
   if (preg_match("/^$__tmp\$/iu", $elm)) {
	$suitable = true;
	break;
   }
  } else {
   if ($v == $elm) {
	$suitable = true;
	break;
   }
  }
}
return $suitable;
}
//
// Return most coincedence available destination by the following parameters...
//
function fn_get_available_destination($location)
{
$country = !empty($location['country']) ? $location['country'] : '';
$state = !empty($location['state']) ? $location['state'] : '';
$zipcode = !empty($location['zipcode']) ? $location['zipcode'] : '';
$city = !empty($location['city']) ? $location['city'] : '';
$address = !empty($location['address']) ? $location['address'] : '';
if (!empty($country)) {
  $state_id = fn_get_state_id($state, $country);
  $city_id = fn_get_city_id($city, $state);

  $condition = '';
  if (AREA == 'C') {
   $condition .= fn_get_localizations_condition('localization');
  }
  $__dests = db_get_array("SELECT a.* FROM ?:destination_elements as a LEFT JOIN ?:destinations as b ON b.destination_id = a.destination_id WHERE b.status = 'A' AND (b.destination_id = 1 OR (1 ?p))", $condition);
  $destinations = array();
  foreach ($__dests as $k => $v) {
   $destinations[$v['destination_id']][$v['element_type']][] = $v['element'];
  }
  $concur_destinations = array();
  foreach ($destinations as $dest_id => $elm_types) {
   // Significance level. The more significance level means the most amount of coincidences
   $significance = 0;
   $dest_countries = !empty($elm_types['C']) ? $elm_types['C'] : array();
   $dest_states = !empty($elm_types['S']) ? $elm_types['S'] : array();
   foreach ($elm_types as $elm_type => $elms) {
	// Check country
	if ($elm_type == 'C') {
	 $suitable = fn_check_element($elms, $country);
	 if ($suitable == false) {
	  break;
	 }
	 $significance += 1 * (1 / count($elms));
	}
  
	// Check state
	if ($elm_type == 'S') {
	 // if country is in destanation_countries and it haven't got states,
	 // we suppose that destanation cover all country
	 if (!in_array($country, $dest_countries) || fn_get_states($country)) {
	  $suitable = fn_check_element($elms, $state_id);
	  if ($suitable == false) {
	   break;
	  }
	 } else {
	  $suitable = true;
	 }
	 $significance += 2 * (1 / count($elms));
	}
	// Check city
	if ($elm_type == 'T') {
	  if (!in_array($state, $dest_states) || fn_get_cities($state)) {
	 $suitable = fn_check_element($elms, $city_id);
	 if ($suitable == false) {
	  break;
	 }
	  } else {
	   $suitable = true;
	 }
	 $significance += 3 * (1 / count($elms));
	}
	// Check zipcode
	if ($elm_type == 'Z') {
	 $suitable = fn_check_element($elms, $zipcode, true);
	 if ($suitable == false) {
	  break;
	 }
	 $significance += 4 * (1 / count($elms));
	}
	// Check address
	if ($elm_type == 'A') {
	 $suitable = fn_check_element($elms, $address, true);
	 if ($suitable == false) {
	  break;
	 }
	 $significance += 5 * (1 / count($elms));
	}
   }
   $significance = number_format($significance, 2, '.', '');
   if ($suitable == true) {
	$concur_destinations[$significance][] = $dest_id;
   }
  }
  if (!empty($concur_destinations)) {
   ksort($concur_destinations, SORT_NUMERIC);
   $concur_destinations = array_pop($concur_destinations);
   return reset($concur_destinations);
  } else {
   return false;
  }
}
return false;
}
//
// Return state ID by it's code and country code
//
function fn_get_state_id($state = '', $country = '')
{
static $state_ids;
if (empty($state) || empty($country)) {
  return false;
}
if (empty($state_ids[$country][$state])) {
  $state_ids[$country][$state] = db_get_field("SELECT state_id FROM ?:states WHERE code = ?s AND country_code = ?s", $state, $country);
}
return $state_ids[$country][$state];
}
function fn_get_city_id($city = '', $state = '')
{
static $city_ids;
if (empty($city) || empty($state)) {
  return false;
}
if (empty($city_ids[$state][$city])) {
  $city_ids[$state][$city] = db_get_field("SELECT city_id FROM ?:cities WHERE city_id = ?s AND state_id = ?s", $city, $state);
}
return $state_ids[$state][$city];
}
I also had to insert a line at controllers/customer/checkout.php near //Get countries and states like that:

$view-&gt;assign('cities', fn_get_all_cities());



then I had to declare the 2 rows for b_city and s_city at profile_fields table, with field_type='K' (mine declaration) so I can recognize the new city selectbox at active_skin/customer/views/profiles/components/profile_fields.tpl by inserting something like that:

{elseif $field.field_type == "K"}  {* City selectbox *}
  <select id="{$id_prefix}elm_{$field.field_id}" class="{if !$skip_field}{$_class}{/if}" name="{$data_name}[{$data_id}]" {if !$skip_field}{$disabled_param}{/if}>
   <option value="">- {$lang.select_city} -</option>
   {* Initializing default cities *}
   {assign var="country_code" value=$settings.General.default_country}
   {assign var="state_code" value=$value|default:$settings.General.default_state}
		    {assign var="city_code" value=$value|default:$settings.General.default_city}
   {if $cities}
    {foreach from=$cities.$state_id item=city}
	 <option {if $city_code == $city.city_id}selected="selected"{/if} value="{$city.city_id}">{$city.code}</option>
    {/foreach}
   {/if}
  </select><input type="text" id="elm_{$field.field_id}_d" name="{$data_name}[{$data_id}]" size="32" maxlength="64" value="{$value}" disabled="disabled" class="input-text hidden {if $_class}disabled{/if}"/>
  <input type="hidden" id="{$id_prefix}elm_{$field.field_id}_default" value="{$state_code}" />

Unfortunately cities cannot be initialised !!

 

as i know cities load async ajax.


In Topic: How To Make City And State As Dropdown List In Cs Cart?

15 April 2018 - 03:00 PM

When customer goes to my checkout page then there is state and city list but you have to give. i want to know that how to make it dropdown?

 

Thank you

You need to edit base tpl. Cities loading by type letter.


In Topic: Why Cs Cart Lacks Of Good Looking (Modern) Themes

15 April 2018 - 02:59 PM

Creating theme its not problem. But need experienced cs-cart expert and good designer.


In Topic: Модуль "оплата После Подтверждения"

22 March 2018 - 05:15 PM

С последней версией cs-cart работает?


In Topic: Correct Multi Lang Work Cs-Cart.

20 February 2018 - 07:49 AM

i'm try to fix its, but not fortunately. Found where redirect doing, fix, but url no working, without - "ru" "en"

Hi,

 

I am having the same issue in our website now.

Kindly advise if you have managed to make you primary domain works only for the language you want: (test.com - russian)

 

I am trying to find any solution for this ..