Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Switch/Case does not work


MLu

Recommended Posts

Posted

I have tried to include a switch/case in /includes/functions/general.php, with the intention to set $country_id and $zone_id based on the language selected by the visitor. The code example below is only test data, but I do not understand why it always selects the "default" value despite of the visitors choice.

 

Is general.php only executed at startup, so that any subsequent changes in $HTTP_GET_VARS['language']) does not reflect, or ???

 

Any help would be greatly appreciated.

 

Mogens

 

// Returns the tax rate for a zone / class
// TABLES: tax_rates, zones_to_geo_zones
function tep_get_tax_rate($class_id, $country_id = -1, $zone_id = -1) {
  global $customer_zone_id, $customer_country_id;

  if ( ($country_id == -1) && ($zone_id == -1) ) {
    if (!tep_session_is_registered('customer_id')) {
// Old code shown below
//      $country_id = STORE_COUNTRY;
//      $zone_id = STORE_ZONE;
// End of old code
// My modified code shown below  
switch ($HTTP_GET_VARS['language']) {
     case 'da':
    $country_id = '57';
       $zone_id = '0';
      break;
      case 'sv':
    $country_id = '203';
       $zone_id = '0';
      break;
default:
    $country_id = '81';
       $zone_id = '0';
      break;
// End of my modified code
}
    } else {
      $country_id = $customer_country_id;
      $zone_id = $customer_zone_id;
    }
  }

Posted
I have tried to include a switch/case in /includes/functions/general.php, with the intention to set $country_id and $zone_id based on the language selected by the visitor. The code example below is only test data, but I do not understand why it always selects the "default" value despite of the visitors choice.

 

Is general.php only executed at startup, so that any subsequent changes in $HTTP_GET_VARS['language']) does not reflect, or ???

 

Any help would be greatly appreciated.

 

Mogens

 

// Returns the tax rate for a zone / class
// TABLES: tax_rates, zones_to_geo_zones
function tep_get_tax_rate($class_id, $country_id = -1, $zone_id = -1) {
  global $customer_zone_id, $customer_country_id;

  if ( ($country_id == -1) && ($zone_id == -1) ) {
    if (!tep_session_is_registered('customer_id')) {
// Old code shown below
//      $country_id = STORE_COUNTRY;
//      $zone_id = STORE_ZONE;
// End of old code
// My modified code shown below  
switch ($HTTP_GET_VARS['language']) {
     case 'da':
    $country_id = '57';
       $zone_id = '0';
      break;
      case 'sv':
    $country_id = '203';
       $zone_id = '0';
      break;
default:
    $country_id = '81';
       $zone_id = '0';
      break;
// End of my modified code
}
    } else {
      $country_id = $customer_country_id;
      $zone_id = $customer_zone_id;
    }
  }

 

 

 

$HTTP_GET_VARS['language'] only contains data after the user selects the language.

Treasurer MFC

Posted
$HTTP_GET_VARS['language'] only contains data after the user selects the language.

 

 

why not use the registered global value $language and $languages_id

Treasurer MFC

Posted

Mogens

I suspect its the variable 'language' you are using in the switch statement.

'language' tends to be the full name (eg english) as in:

require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_CATEGORIES);

 

You are trying to use the two letter code (eg 'da'). I think you should use 'languages_code' instead. See application_top.php:

...

// set the language

if (!tep_session_is_registered('language') || isset($HTTP_GET_VARS['language'])) {

if (!tep_session_is_registered('language')) {

tep_session_register('language');

tep_session_register('languages_id');

tep_session_register('languages_code');

}

 

include(DIR_WS_CLASSES . 'language.php');

$lng = new language();

 

if (isset($HTTP_GET_VARS['language']) && tep_not_null($HTTP_GET_VARS['language'])) {

$lng->set_language($HTTP_GET_VARS['language']);

} else {

$lng->get_browser_language();

}

 

$language = $lng->language['directory'];

$languages_id = $lng->language['id'];

$languages_code = $lng->language['code'];

}

...

 

Hope this helps,

Martin

Posted

Thanks for the replies. However, I am still a little lost.

I assume that the code in general.php, where I have tried to manipulate language_id and zone_id, is the correct place to do this to achieve my objective to let the pre-login taxrate applied be identical to the tax rates in the countries I choose?

 

It is correct that language_id and zone_id only appears to be set if and when the user actively selects a language. Of course I also need to take into account the language chosen from browser language. But that I have left for "round 2". Basically I would like to know if I have targeted the correct code (general.php) and using the correct syntax in the Switch/Case statement. However, and idea on how to get info about the language chosen from the browser setttings would also be needed at some time.

 

Martin: I tried to display the content of language_id after I had selected for instance Germany. The value of language_id was "ge", so I believe that language_id contains a two-character language code.

 

Mogens

Posted

Hmm that's interesting. languages_id is the number in the table. I'm not sure what language_id is set to be but the code for german is actually 'de' as in deutsch.

Try using languages_code (with the s) in the switch statement. You can get all the codes by doing a select * on the languages table.

languages_code (as well as languages_id) is set in application_top.php (see lines 112 onwards).

I think you are targeting the correct code and syntactically the switch statement is correct. The problem lies with the values you are using in the case().

I hope this helps.

Martin

Posted
Thanks for the replies. However, I am still a little lost.

I assume that the code in general.php, where I have tried to manipulate language_id and zone_id, is the correct place to do this to achieve my objective to let the pre-login taxrate applied be identical to the tax rates in the countries I choose?

 

It is correct that language_id and zone_id only appears to be set if and when the user actively selects a language. Of course I also need to take into account the language chosen from browser language. But that I have left for "round 2". Basically I would like to know if I have targeted the correct code (general.php) and using the correct syntax in the Switch/Case statement. However, and idea on how to get info about the language chosen from the browser setttings would also be needed at some time.

 

Martin: I tried to display the content of language_id after I had selected for instance Germany. The value of language_id was "ge", so I believe that language_id contains a two-character language code.

 

Mogens

 

the languages variables are registered ones in application_top. those variables exist as long as the session exists. HTTP_GET_VARS is an array which only contains the variables after you come from another page. These do not last beyond that.

 

so use the $language variable which contains the directory name of the active language like 'english' or 'german' or the $languages_id which contains the number of the active language like 1, 2, 3 etc.

these variables are always present and ofcourse are updated when the user switches the language.

Treasurer MFC

Posted

Amanda: You are right - $languages_id is a much better choice. I have tested it out, and this variable is set even if the user does not actively change language.

 

But still, it seems as if the program ignores the switch / case which now looks like this:

 

// Returns the tax rate for a zone / class
// TABLES: tax_rates, zones_to_geo_zones
 function tep_get_tax_rate($class_id, $country_id = -1, $zone_id = -1) {
   global $customer_zone_id, $customer_country_id;

   if ( ($country_id == -1) && ($zone_id == -1) ) {
     if (!tep_session_is_registered('customer_id')) {
// Old code shown below
//      $country_id = STORE_COUNTRY;
//      $zone_id = STORE_ZONE;
// End of old code
// My modified code shown below  
 switch ($languages_id) { 
    	 case '4' : //languages_id 4 = Danish & country_id 57 = Denmark - tax rate = 25%
     $country_id = '57';
      	 $zone_id = '0';
       break; 
       case '7' : //languages_id 7 = German & country_id 81 = Germany - tax rate = 7%
     $country_id = '81';
      	 $zone_id = '0';
       break; 
 default: //languages_id "rest"  & country_id 203 = Norway - tax rate = 0%
     $country_id = '160';
      	 $zone_id = '0';
       break; 
// End of my modified code
 } 
     } else {
       $country_id = $customer_country_id;
       $zone_id = $customer_zone_id;
     }
   }

 

The last (=default) section of the case is executed, so I always end up with a 0% tax rate (with the current parameters). Strange as my browser language is 4, so I would have expected the first case to have been chosen.

 

Mogens

Posted

Mogens,

What is your default language in osCommerce set to ?

If its english (languages_id = 1) then that would be your problem. The browser language is only used if the language variable is not set. From application_top.php line 123-

if (isset($HTTP_GET_VARS['language']) && tep_not_null($HTTP_GET_VARS['language'])) {

$lng->set_language($HTTP_GET_VARS['language']);

} else {

$lng->get_browser_language();

}

 

To test, put 'echo $languages_id' prior to the switch statement to find out the value of $languages_id.

 

Martin

Posted

My default language in Admin > Localization > Languages was set to English. However, I already have tried to echo out $languages_id, and it showed '4' (= Danish = my current browser language) despite of the default language. Now I have changed the default language to "Danish" but the problem still persist.

 

Mogens

Posted

Hmm....well if you put the echo $languages_id on the line immediately above the switch statement and it said 4 and you are 100% sure that you are going to the default part of the switch statement then that is really weird.

Silly question I know but I guess you've also checked the settings of country_id and zone_id prior to the select statement and verified that the tax rate result you are getting is consistent with what's in the database ?

Posted

Thanks Martin. I probably need to dig a little deeper into how to debug php-code. I am not exactly an expert :-), and I have to find out how and where to print out variable values at the time of execution. I guess this would require spooling results to some kind of test file, but I have to do some studying first.

 

Thanks so far!

 

Mogens

Posted

Martin: Via error_log() I have now tried to spool the value of $languages_id before executing the switch/case. And you were right, it seems as if the value is "".

 

I guess this would indicate that application_top.php is executed after general.php. From your earlier suggestion, should I then try to include the code from application_top.php in general.php just before executing the switch/case? And if positive, how should this be done?

 

Not knowing the structure of osCommerce, this is becoming a little guesswork from my side.

 

Thanks for your help!

 

Mogens

Posted
Amanda: You are right - $languages_id is a much better choice. I have tested it out, and  this variable is set even if the user does not actively change language.

 

But still, it seems as if the program ignores the switch / case which now looks like this:

 

// Returns the tax rate for a zone / class
// TABLES: tax_rates, zones_to_geo_zones
 function tep_get_tax_rate($class_id, $country_id = -1, $zone_id = -1) {
   global $customer_zone_id, $customer_country_id;

   if ( ($country_id == -1) && ($zone_id == -1) ) {
     if (!tep_session_is_registered('customer_id')) {
// Old code shown below
//      $country_id = STORE_COUNTRY;
//      $zone_id = STORE_ZONE;
// End of old code
// My modified code shown below  
 switch ($languages_id) { 
    	 case '4' : //languages_id 4 = Danish & country_id 57 = Denmark - tax rate = 25%
     $country_id = '57';
      	 $zone_id = '0';
       break; 
       case '7' : //languages_id 7 = German & country_id 81 = Germany - tax rate = 7%
     $country_id = '81';
      	 $zone_id = '0';
       break; 
 default: //languages_id "rest"  & country_id 203 = Norway - tax rate = 0%
     $country_id = '160';
      	 $zone_id = '0';
       break; 
// End of my modified code
 } 
     } else {
       $country_id = $customer_country_id;
       $zone_id = $customer_zone_id;
     }
   }

 

The last (=default) section of the case is executed, so I always end up with a 0% tax rate (with the current parameters).  Strange as my browser language is 4, so I would have expected the first case to have been chosen.

 

Mogens

 

 

 

because you put it in a function:

 

add $languages_id to your global declarations :

 

global $customer_zone_id, $customer_country_id;

Treasurer MFC

Posted

Mogens,

Amanda is right. The problem is because $languages_id seems not to be known by this function (so it automatically sees it as an empty value). So maybe making languages_id a global is the simplest (quick fix) way to do it.

 

However, this should not be necessary. According to application_top (the way I understand it) if you have a language is defined when you start your application (& before you call this function), then the languages_id variable should be accessible for the life of the session (effectively be in a cookie) - therfore the function would know the languages_id variable: (from application_top):

if (!tep_session_is_registered('language') || isset($HTTP_GET_VARS['language'])) {

if (!tep_session_is_registered('language')) {

tep_session_register('language');

tep_session_register('languages_id');

 

If you are just running a page where there is no language selection then that is why your function can't get a value for languages_id.

 

Btw re your comment:

"I guess this would indicate that application_top.php is executed after general.php. From your earlier suggestion, should I then try to include the code from application_top.php in general.php just before executing the switch/case? And if positive, how should this be done?"

 

The reason I show snippets from application_top is to illustrate what I mean.

Check that the top level page (ie the php file you call from the browser) includes application_top (with the line: require('includes/application_top.php'); line).

If so then application_top itself includes general.php - which is why your page recognises the function tep_get_tax_rate(). This means you don't have to do any more including of code.

 

I'm no oscommerce expert - but because I'm spending quite a bit of time significantly customising it, I'm learning quickly !

 

Hope this all helps,

Martin

Posted
Mogens,

Amanda is right. The problem is because $languages_id seems not to be known by this function (so it automatically sees it as an empty value). So maybe making languages_id a global is the simplest (quick fix) way to do it.

 

However, this should not be necessary. According to application_top (the way I understand it) if you have a language is defined when you start your application (& before you call this function), then the languages_id variable should be accessible for the life of the session (effectively be in a cookie) - therfore the function would know the languages_id variable: (from application_top):

  if (!tep_session_is_registered('language') || isset($HTTP_GET_VARS['language'])) {

    if (!tep_session_is_registered('language')) {

      tep_session_register('language');

      tep_session_register('languages_id');

 

If you are just running a page where there is no language selection then that is why your function can't get a value for languages_id.

 

Btw re your comment:

"I guess this would indicate that application_top.php is executed after general.php. From your earlier suggestion, should I then try to include the code from application_top.php in general.php just before executing the switch/case? And if positive, how should this be done?"

 

The reason I show snippets from application_top is to illustrate what I mean.

Check that the top level page (ie the php file you call from the browser) includes application_top (with the line:  require('includes/application_top.php'); line).

If so then application_top itself includes general.php - which is why your page recognises the function tep_get_tax_rate(). This means you don't have to do any more including of code.

 

I'm no oscommerce expert - but because I'm spending quite a bit of time significantly customising it, I'm learning quickly !

 

Hope this all helps,

Martin

 

 

well, I am also still learning but I beg to differ.

 

$languages_id is accessible without declaring it global everywhere but not within a function definition. The function only sees the variables declared in it or passed to it.

 

the global declaration to $languages_id effectively creates a reference to the session variable available outside of the function.

 

but correct me if I am mistaken.

Treasurer MFC

Posted
well, I am also still learning but I beg to differ.

 

$languages_id is accessible without declaring it global everywhere but not within a function definition. The function only sees the variables declared in it or passed to it.

 

the global declaration to $languages_id effectively creates a reference to the session variable available outside of the function.

 

but correct me if I am mistaken.

 

 

otherwise you need to reference it directly by $_SESSION['languages_id'];

 

I think.

Treasurer MFC

Posted
otherwise you need to reference it directly by $_SESSION['languages_id'];

 

I think.

 

 

from the manual

 

<?php

$a = 1; /* global scope */

 

function Test()

{

echo $a; /* reference to local scope variable */

}

 

Test();

?>

 

This script will not produce any output because the echo statement refers to a local version of the $a variable, and it has not been assigned a value within this scope. You may notice that this is a little bit different from the C language in that global variables in C are automatically available to functions unless specifically overridden by a local definition. This can cause some problems in that people may inadvertently change a global variable. In PHP global variables must be declared global inside a function if they are going to be used in that function. An example:

 

<?php

$a = 1;

$b = 2;

 

function Sum()

{

global $a, $b;

 

$b = $a + $b;

}

 

Sum();

echo $b;

?>

 

The above script will output "3". By declaring $a and $b global within the function, all references to either variable will refer to the global version. There is no limit to the number of global variables that can be manipulated by a function.

Treasurer MFC

Posted

Amanda: Yep you are right - global in a php function does allow it to access a global variable. That's different to other languages I have used...so that's something I have learnt today :D

 

Mogens: Ignore my suggestions above - as Amanda says make the global line be:

global $customer_zone_id, $customer_country_id, $languages_id;

 

Martin

Posted

Martin and Amanda: Thank you very very much!! I am an extremely happy man now :D :D

 

Declaring @languages_id as global did the trick. Works brilliantly now!

 

In fact this problem has cost me a lot of lost sales during the past 6 months, and a lot of customer enquiries where I had to explain a tax issue that was not that easy to explain.

 

Regards,

Mogens

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...