Author Topic: Ship To Address State Name instead of State Code 2  (Read 1592 times)

siowwf

  • Beginner
  • *
  • Posts: 3
Ship To Address State Name instead of State Code 2
« on: October 04, 2011, 21:26:45 pm »
Hi there! I am new here but having searched through the entire web, found lots of the same questions but nobody provide the solutions.

How can I change the Ship To address' State/Province/Region to display the state_name instead of state_code_2?

I have done the changes on checkout page, but just can't find the right place to change this.

Anybody there please help?

TQ!  :-[ :-[ :-[

[attachment cleanup by admin]

siowwf

  • Beginner
  • *
  • Posts: 3
Re: Ship To Address State Name instead of State Code 2
« Reply #1 on: October 07, 2011, 13:03:43 pm »
I have found the solution myself.

Edited a few tables on the database: jos_vm_state, jos_vm_order_user_info, jos_vm_user_info. Set "datatype" of the state_code_2 columns to VARCHAR(64). Modify the previous recorded data for example, KL to Kuala Lumpur as now it can accommodate more than 2 characters.

Some existing data such needed to be changed one by one (if any) to appear the full state name on the order detail screen.

Good luck to those facing the same problem!

 8)

samoacat

  • Beginner
  • *
  • Posts: 1
Re: Ship To Address State Name instead of State Code 2
« Reply #2 on: November 29, 2011, 13:59:53 pm »
Hi there.

I just came across the same problem and solved it. I post here my solution which does not involve any database changes, just a hack in code. Though I am an experienced programmer, I am not a Joomla/Virtuemart/PHP expert, so there may be bugs/flaws in this solution: use it at your own risk.

BTW, I'm using Joomla 1.5.18 and VirtueMart 1.1.5


I am using a customized classic_1 template, so first file I modified is found in (change your template name accordingly):

yoursitename/components/com_virtuemart/themes/classic_1/templates/pages/account.order_details.tpl.php


Around line 103 there's the following switch:

Code: [Select]
switch($field->name) {
          case 'country':
          require_once(CLASSPATH.'ps_country.php');
          $country = new ps_country();
          $dbc = $country->get_country_by_code($dbbt->f($field->name));
          if( $dbc !== false ) echo $dbc->f('country_name');
          break;

          default:
          echo $dbbt->f($field->name);
          break;
          }

replace it by:

Code: [Select]
switch($field->name) {
          case 'country':
          require_once(CLASSPATH.'ps_country.php');
          $country = new ps_country();
          $dbc = $country->get_country_by_code($dbbt->f($field->name));
          if( $dbc !== false ) echo $dbc->f('country_name');
          break;
// Added code for translating state code into state name
case 'state':
   require_once(CLASSPATH.'ps_country.php');
   $country = new ps_country();
   // we must specify the country_id corresponding to this state as there can be more than one state using the same 2-3 digit code
   $dbc = $country->get_country_by_code($dbbt->f('country'));
           if( $dbc !== false) $country_id = $dbc->f('country_id');
   $dbc = $country->get_state_by_code($country_id, $dbbt->f($field->name));
   if( $dbc !== false) echo $dbc->f('state_name');
   break;
          default:
          echo $dbbt->f($field->name);
          break;
          }


Explanation: This switch is inside a foreach that echoes all the user registered fields, and is meant to translate the country code by the country name. As you can see I just added a similar 'case' instruction for the state field.


PS: The same switch can be found in the file:

yoursitename/administrator/components/com_virtuemart/html/order.order_printdetails.php

but for some reason modifying this file does not affect my PO output, so I left this one untouched.



Now its time to add the new get_state_by_code function into:

yoursitename/administrator/components/com_virtuemart/classes/ps_country.php


under the "get_country_by_code" function i added this new function:

Code: [Select]

//New function that returns state table fields corresponding to the specified country_id and state code (can be 2 or 3 digit code)
function &get_state_by_code( $idcountry, $state_code ) {
$db = new ps_DB();
$state_code_type = strlen( $state_code );
switch ($state_code_type) {
case 2:
$state_code_type_field = 'state_2_code';
break;
case 3:
$state_code_type_field = 'state_3_code';
break;
default:
return false;
}
$db->query('SELECT `country_id`, `state_name`, `state_2_code`, `state_3_code`
FROM `#__{vm}_state` WHERE `'.$state_code_type_field.'` = \''.$db->getEscaped($state_code).
'\' AND country_id = \''.$db->getEscaped($idcountry).'\'' );
$db->next_record();
return $db;
}

Explanation: Just copy/pasted the get_country_by_code, changing necessary fields and variables to suit the state table (in this case I hardcoded the table name as `#__{vm}_state` as $this->getTable() returns always country tablename as we are inside a ps_country object), and adding the country_id AND clause.


 

I hope this will save some time to anyone with the same problem,


Best regards

samoacat