Zen Cart Logo
Forums / Managing Customers and Orders / City value of States

City value of States

Views: 3,088

Results 1 to 16 of 16
5 Aug 2017, 2:49 AM
#1
gunni avatar

gunni

Zen Follower

Join Date:
Aug 2010
Location:
Israel
Posts:
287
Plugin Contributions:
0

City value of States

Hi
When a customer creates an account I need a pull down menu for cities.
I can show the customer the pull down menu of the states and call it "city" but
I need Zen to get this chosen state as a city value.
I tried to play with the file tpl_modules_create_account.php without much success.

Here is the original code of the city:

<label class="inputLabel" for="city"><?php echo ENTRY_CITY; ?></label>
<?php echo zen_draw_input_field('city', '', zen_set_field_length(TABLE_ADDRESS_BOOK, 'entry_city', '40') . ' id="city" placeholder="' . ENTRY_CITY_TEXT . '"'. ((int)ENTRY_CITY_MIN_LENGTH > 0 ? '' : '')); ?>
<br class="clearBoth" />

And here is the original code of the states:

<?php
  if (ACCOUNT_STATE == 'true') {
    if ($flag_show_pulldown_states == true) {
?>
<label class="inputLabel" for="stateZone" id="zoneLabel"><?php echo ENTRY_STATE; ?></label>
<?php
      echo zen_draw_pull_down_menu('zone_id', zen_prepare_country_zones_pull_down($selected_country), $zone_id, 'id="stateZone"');
      if (zen_not_null(ENTRY_STATE_TEXT)) echo ' <span class="alert">' . ENTRY_STATE_TEXT . '</span>';
    }
?>

<?php if ($flag_show_pulldown_states == true) { ?>
<br class="clearBoth" id="stBreak" />
<?php } ?>
<label class="inputLabel" for="state" id="stateLabel"><?php echo $state_field_label; ?></label>
<?php
    echo zen_draw_input_field('state', '', zen_set_field_length(TABLE_ADDRESS_BOOK, 'entry_state', '40') . ' id="state" placeholder="' . ENTRY_STATE_TEXT . '"');
    if ($flag_show_pulldown_states == false) {
      echo zen_draw_hidden_field('zone_id', $zone_name, ' ');
    }
?>
<br class="clearBoth" />
5 Aug 2017, 11:44 AM
#2
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,102
Plugin Contributions:
56

Re: City value of States

Do you need the state value to be part of the address, too? In what format is the address displayed?

5 Aug 2017, 12:27 PM
#3
gunni avatar

gunni

Zen Follower

Join Date:
Aug 2010
Location:
Israel
Posts:
287
Plugin Contributions:
0

Re: City value of States

lat9:

Do you need the state value to be part of the address, too? In what format is the address displayed?

No. No need the state value.
But I need to use the zone_id to be the "city" value.
Here is the page link (in Hebrew).
Instead of the text "state" I wrote "city" in Hebrew (עיר) so the customer needs to choose his city from the drop down list.
Behind the sceen I need to get his "city" value from the state value.
This I learnt after a customer tried to make paypal payment but paypal sent an error that the customer has no city...

5 Aug 2017, 5:13 PM
#4
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,102
Plugin Contributions:
56

Re: City value of States

@gunni, I'm still not sure what you need to do.

Is it that you want the customer to choose a city (via the associated zone_id dropdown) and then use that city as both the city and state for the customer's address?

5 Aug 2017, 7:50 PM
#5
gunni avatar

gunni

Zen Follower

Join Date:
Aug 2010
Location:
Israel
Posts:
287
Plugin Contributions:
0

Re: City value of States

lat9:

@gunni, I'm still not sure what you need to do.

Is it that you want the customer to choose a city (via the associated zone_id dropdown) and then use that city as both the city and state for the customer's address?

No need for states.

To make the story long:
I have a category in the site with products that can not be shipped to some places in the country.
They can be delivered only for some near cities.
In order to make it happen, I put some code of Ajeh in the shipping module which says that if
the cart contains products from category X and the shipping is to Y, then no way.
Y is the zone_id.
and in order to decide if the "city" is good for delivery or not, I need to know the city id and can not
let the customer write the city as he likes. In this situation there will be too many options for same city
(like new york or new-york etc').
So I used the database of the states and for each "state" I put its' id number in the shipping module.
The customer do not know that he chose a state. He thinks that he choose a city.
I do not need the state as a state.
The only problem is that now, I get the customer's state instead of city and paypal not accepting it.
If I did not clear myself, please let me know.

5 Aug 2017, 8:12 PM
#6
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,102
Plugin Contributions:
56

Re: City value of States

When you look at a customer's record in the admin, does it show the "City" as empty?

Perhaps you need a small change to the "create_account" page's processing, where you use the zone_id value to look up the associated "City" and just copy that value into the customer's "City" field.

5 Aug 2017, 8:23 PM
#7
gunni avatar

gunni

Zen Follower

Join Date:
Aug 2010
Location:
Israel
Posts:
287
Plugin Contributions:
0

Re: City value of States

lat9:

When you look at a customer's record in the admin, does it show the "City" as empty?

Perhaps you need a small change to the "create_account" page's processing, where you use the zone_id value to look up the associated "City" and just copy that value into the customer's "City" field.

Yes. The city is empty in the customer's record.

Tried some changes in the "create account" without success. The city field in the customer's record still empty.
Even tried city=state but I'm not a PHP expert...
That's why I put in the beginning of this thread the "create_account" relevant code of the city and the state so you could advice
what value should I put where...

6 Aug 2017, 12:01 PM
#8
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,102
Plugin Contributions:
56

Re: City value of States

You should keep the changes you made to the tpl_modules_create_account.php (especially the renaming of the "State" field to "City").

If you don't have a file named /includes/modules/responsive_sheffield_blue/create_account.php, create a template override by copying the like-named file from the /includes/modules directory into the responsive_sheffield_blue subdirectory.

You'll need to edit that template-override file, looking for the following block (for zc1.5.5a, around line 200) that deals with the country/state values:

  if (strlen($city) < ENTRY_CITY_MIN_LENGTH) {
    $error = true;
    $messageStack->add('create_account', ENTRY_CITY_ERROR);
  }

  if (ACCOUNT_STATE == 'true') {
    $check_query = "SELECT count(*) AS total
                    FROM " . TABLE_ZONES . "
                    WHERE zone_country_id = :zoneCountryID";
    $check_query = $db->bindVars($check_query, ':zoneCountryID', $country, 'integer');
    $check = $db->Execute($check_query);
    $entry_state_has_zones = ($check->fields['total'] > 0);
    if ($entry_state_has_zones == true) {
      $zone_query = "SELECT distinct zone_id, zone_name, zone_code
                     FROM " . TABLE_ZONES . "
                     WHERE zone_country_id = :zoneCountryID
                     AND " .
                     ((trim($state) != '' && $zone_id == 0) ? "(upper(zone_name) like ':zoneState%' OR upper(zone_code) like '%:zoneState%') OR " : "") .
                    "zone_id = :zoneID
                     ORDER BY zone_code ASC, zone_name";

      $zone_query = $db->bindVars($zone_query, ':zoneCountryID', $country, 'integer');
      $zone_query = $db->bindVars($zone_query, ':zoneState', strtoupper($state), 'noquotestring');
      $zone_query = $db->bindVars($zone_query, ':zoneID', $zone_id, 'integer');
      $zone = $db->Execute($zone_query);

      //look for an exact match on zone ISO code
      $found_exact_iso_match = ($zone->RecordCount() == 1);
      if ($zone->RecordCount() > 1) {
        while (!$zone->EOF && !$found_exact_iso_match) {
          if (strtoupper($zone->fields['zone_code']) == strtoupper($state) ) {
            $found_exact_iso_match = true;
            continue;
          }
          $zone->MoveNext();
        }
      }

      if ($found_exact_iso_match) {
        $zone_id = $zone->fields['zone_id'];
        $zone_name = $zone->fields['zone_name'];
      } else {
        $error = true;
        $error_state_input = true;
        $messageStack->add('create_account', ENTRY_STATE_ERROR_SELECT);
      }
    } else {
      if (strlen($state) < ENTRY_STATE_MIN_LENGTH) {
        $error = true;
        $error_state_input = true;
        $messageStack->add('create_account', ENTRY_STATE_ERROR);
      }
    }
  }

... and make the highlighted changes, copying the matching zone's name into the city value.

[B]/*-bof --- City will be copied from the matching state ---[/B]
  if (strlen($city) < ENTRY_CITY_MIN_LENGTH) {
    $error = true;
    $messageStack->add('create_account', ENTRY_CITY_ERROR);
  }
[B]-eof --- City will be copied from the matching state --- */[/B]

  if (ACCOUNT_STATE == 'true') {
    $check_query = "SELECT count(*) AS total
                    FROM " . TABLE_ZONES . "
                    WHERE zone_country_id = :zoneCountryID";
    $check_query = $db->bindVars($check_query, ':zoneCountryID', $country, 'integer');
    $check = $db->Execute($check_query);
    $entry_state_has_zones = ($check->fields['total'] > 0);
    if ($entry_state_has_zones == true) {
      $zone_query = "SELECT distinct zone_id, zone_name, zone_code
                     FROM " . TABLE_ZONES . "
                     WHERE zone_country_id = :zoneCountryID
                     AND " .
                     ((trim($state) != '' && $zone_id == 0) ? "(upper(zone_name) like ':zoneState%' OR upper(zone_code) like '%:zoneState%') OR " : "") .
                    "zone_id = :zoneID
                     ORDER BY zone_code ASC, zone_name";

      $zone_query = $db->bindVars($zone_query, ':zoneCountryID', $country, 'integer');
      $zone_query = $db->bindVars($zone_query, ':zoneState', strtoupper($state), 'noquotestring');
      $zone_query = $db->bindVars($zone_query, ':zoneID', $zone_id, 'integer');
      $zone = $db->Execute($zone_query);

      //look for an exact match on zone ISO code
      $found_exact_iso_match = ($zone->RecordCount() == 1);
      if ($zone->RecordCount() > 1) {
        while (!$zone->EOF && !$found_exact_iso_match) {
          if (strtoupper($zone->fields['zone_code']) == strtoupper($state) ) {
            $found_exact_iso_match = true;
            continue;
          }
          $zone->MoveNext();
        }
      }

      if ($found_exact_iso_match) {
        $zone_id = $zone->fields['zone_id'];
        $zone_name = $zone->fields['zone_name'];
[B]        $city = $zone_name;  // Copy the city from the selected zone's name[/B]
      } else {
        $error = true;
        $error_state_input = true;
        $messageStack->add('create_account', [B]ENTRY_STATE_ERROR_SELECT[/B]);
      }
    } else {
      if (strlen($state) < ENTRY_STATE_MIN_LENGTH) {
        $error = true;
        $error_state_input = true;
        $messageStack->add('create_account', [B]ENTRY_STATE_ERROR[/B]);
      }
    }
  }

That should do it for the coding part (please note that I haven't tested this, so be sure to attempt the changes on a test site!). You'll also need to change the wording of the two (currently state-related) messages that I **highlighted **above.

You'll also want to apply these changes to the address-gathering forms and processing for the checkout_payment_address, checkout_shipping_address and address_book_process page.

6 Aug 2017, 1:01 PM
#9
gunni avatar

gunni

Zen Follower

Join Date:
Aug 2010
Location:
Israel
Posts:
287
Plugin Contributions:
0

Re: City value of States

Thank you lat9 !
First process for the account create looks good.
I put the code $city = $zone_name and after a new customer create, I look in the admin and the customer record
city apears good !
Now, need to work on the other 3 address-payment related files.
Hope no problems there....

6 Aug 2017, 2:09 PM
#10
gunni avatar

gunni

Zen Follower

Join Date:
Aug 2010
Location:
Israel
Posts:
287
Plugin Contributions:
0

Re: City value of States

lat9:

You'll also want to apply these changes to the address-gathering forms and processing for the checkout_payment_address, checkout_shipping_address and address_book_process page.

@lat9
regarding those 3 files that you mentioned, I thought that I need to put in them the code "$city = $zone_name"
but they have mainly 'defines' in them.
Did you mean to change the defines?

On the other hand, there is checkout_new_address.php file and there I add the "$city = $zone_name".

So...just want to be sure that "$city = $zone_name" was needed to be put in 2 files only.

6 Aug 2017, 3:03 PM
#11
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,102
Plugin Contributions:
56

Re: City value of States

@gunni, for the checkout_payment_address and checkout_shipping_address pages' update you'll need to start with a template-override version of /includes/modules/checkout_new_address.php and apply the same changes as you did with the template-override of /includes/modules/create_account.php.

That will provide the "city" recording for both pages and you'll need also to update your template-override version of tpl_checkout_payment_address_default.php and tpl_checkout_shipping_address_default.php like you did to tpl_modules_create_account.php.

Then, for the address_book_process page you'll edit the core-file /includes/modules/pages/address_book_process/header_php.php like you did for /includes/modules/create_account.php and update your template-override version of tpl_address_book_process_default.php to make the template change.

I think that should "do it"!

6 Aug 2017, 4:37 PM
#12
gunni avatar

gunni

Zen Follower

Join Date:
Aug 2010
Location:
Israel
Posts:
287
Plugin Contributions:
0

Re: City value of States

@lat9 Big thanks again but not all clear....

lat9:

@gunni, for the checkout_payment_address and checkout_shipping_address pages' update you'll need to start with a template-override version of /includes/modules/checkout_new_address.php and apply the same changes as you did with the template-override of /includes/modules/create_account.php.

That will provide the "city" recording for both pages

This is clear (after 6 times or reading...:).

lat9:

and you'll need also to update your template-override version of tpl_checkout_payment_address_default.php and tpl_checkout_shipping_address_default.php like you did to tpl_modules_create_account.php.

I opened thoese two files and they do not have "city" nor "state" like I changed the tpl_modules_create_account.php file
so what do I need to change in these files ? Not clear.

lat9:

Then, for the address_book_process page you'll edit the core-file /includes/modules/pages/address_book_process/header_php.php like you did for /includes/modules/create_account.php

Did it :)

lat9:

and update your template-override version of tpl_address_book_process_default.php to make the template change.

What to update ? Opened this file and do not understand what to update...where ? Or should I put this file in my template dir ?

Thanks again...

6 Aug 2017, 5:46 PM
#13
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,102
Plugin Contributions:
56

Re: City value of States

gunni:

I opened thoese two files and they do not have "city" nor "state" like I changed the tpl_modules_create_account.php file
so what do I need to change in these files ? Not clear.
My bad; those two files (tpl_checkout_payment_address_default.php and tpl_checkout_shipping_address_default.php) both include /tpl_modules_checkout_new_address.php; it's that common file that you might need to update to change the "State" to "City" ... although, I'm thinking now that those values are in your primary language file (hebrew.php?).

gunni:

What to update ? Opened this file and do not understand what to update...where ? Or should I put this file in my template dir ?

Thanks again...
Dang it! That one, too. The form is displayed via tpl_modules_address_book_details.php

For both the tpl_modules_address_book_details.php and tpl_modules_checkout_new_address.php, you'll need to copy the files from the /includes/templates/template_default/templates directory to /includes/templates/responsive_sheffield_blue/templates ... unless the file is already in that directory.

In either case, you'll apply any edits like you previously made to tpl_modules_create_account.php.

6 Aug 2017, 7:02 PM
#14
gunni avatar

gunni

Zen Follower

Join Date:
Aug 2010
Location:
Israel
Posts:
287
Plugin Contributions:
0

Re: City value of States

lat9:

My bad; those two files (tpl_checkout_payment_address_default.php and tpl_checkout_shipping_address_default.php) both include /tpl_modules_checkout_new_address.php; it's that common file that you might need to update to change the "State" to "City" ... although, I'm thinking now that those values are in your primary language file (hebrew.php?).

Dang it! That one, too. The form is displayed via tpl_modules_address_book_details.php

For both the tpl_modules_address_book_details.php and tpl_modules_checkout_new_address.php, you'll need to copy the files from the /includes/templates/template_default/templates directory to /includes/templates/responsive_sheffield_blue/templates ... unless the file is already in that directory.

In either case, you'll apply any edits like you previously made to tpl_modules_create_account.php.

Now all is clear ! Much appreciate your kind help lat9!
By the way....next time... would it be easier and more "clean" to make the city its own drop down menu with its database
instead of this state-city exchanges ?

6 Aug 2017, 7:25 PM
#15
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,102
Plugin Contributions:
56

Re: City value of States

gunni:

Now all is clear ! Much appreciate your kind help lat9!
By the way....next time... would it be easier and more "clean" to make the city its own drop down menu with its database
instead of this state-city exchanges ?
gunni, it depends! As long as you've *commented the heck *out of what you're doing -- especially the changes to core and/or template files -- there shouldn't be an issue.

There would be a bunch more admin-tool work needed to create and manage that separate database table for the cities, IMO, given that the *zones *table can be "reused" for your stores' special case.

6 Aug 2017, 7:34 PM
#16
gunni avatar

gunni

Zen Follower

Join Date:
Aug 2010
Location:
Israel
Posts:
287
Plugin Contributions:
0

Re: City value of States

Thanks again lat9:thumbsup: