Author Topic: [SOLVED] Minor - but annoying - bug in models/country.php  (Read 3790 times)

mb000000

  • Beginner
  • *
  • Posts: 32
[SOLVED] Minor - but annoying - bug in models/country.php
« on: August 14, 2013, 12:51:41 pm »
J2.5.14 & VM2.0.22a

I am working on updating someone else's plugin to support VM2 and at the same time am adding some additional functionality - the plugin allows for address lookup based on postcode, but only works for the UK.  Part of the additional functionaliy I am adding is to test what country the visitor has entered and thence only display the look up if the country is the UK.  There are many solutions to determining whether the UK was selected, but I thought I would use the VirtuemartModelCountry class to look up based on the ISO defined (and therefore largely immutable) "GBR".

Firstly I see that that class is not apparently used within VM itself (nor by any of the plugins I have installed) and secondly I see that at least one of the functions doesn't work - getCountryByCode.  This returns a country DB object based on a match with either the 2 or 3 letter code.  Here's the relevent snippet:
Code: [Select]
$countryCodeLength = strlen($code);
switch ($countryCodeLength) {
    case 2:
$countryCodeFieldname = 'country_2_code';
break;
    case 3:
$countryCodeFieldname = 'country_3_code';
break;
    default:
return false;
}

$query = 'SELECT *';
$query .= ' FROM `#__virtuemart_countries`';
$query .= ' WHERE `' . $countryCodeFieldname . '` = ' . (int)$code;
However, note that above the $query casts the $code to an int (when it is always expected to be a string), so the query always returns rubbish.

I suggest the query should be something like:
Code: [Select]
$query = 'SELECT *';
$query .= ' FROM `#__virtuemart_countries`';
$query .= ' WHERE `' . $countryCodeFieldname . '` = ' .  "'". $code ."'";

Mark

alatak

  • Contributing Developer
  • Full Member
  • *
  • Posts: 2048
    • alatak.net
Re: Minor - but annoying - bug in models/country.php
« Reply #1 on: August 14, 2013, 19:13:39 pm »
Hello

We probably did not notice, because as you said it is not used.
We use this one ShopFunctions::getCountryIDByName

I have fixed the getCountryByCode function. Txs