VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Topic started by: mb000000 on August 14, 2013, 12:51:41 PM

Title: [SOLVED] Minor - but annoying - bug in models/country.php
Post by: mb000000 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:
$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:
$query = 'SELECT *';
$query .= ' FROM `#__virtuemart_countries`';
$query .= ' WHERE `' . $countryCodeFieldname . '` = ' .  "'". $code ."'";


Mark
Title: Re: Minor - but annoying - bug in models/country.php
Post by: alatak 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