简体   繁体   中英

How to get country iso code by country name in prestashop

I'm creating my own module in prestashop 1.7 which is for payment gateway and works when checkout button is created and my payment gateway is selected.

In that plugins I needed some fields to use for some purpose for that I need user name, phone, email and country iso code.

I'm getting user name, phone and email in those variables :

$customerDetails = $this->context->customer;

$address = new Address($this->context->cart->id_address_delivery);

I'm also getting country name & country id as well but what I need is country ISO code. I saw prestashop have table for it in their database and they have ISO codes as well but I can't find any method or recommended way to get country ISO code by country name or country id.

You can use the Country Class method public static function getIsoById($idCountry) .

So you could do something like:

$country_iso = Country::getIsoById([THE_ID]);

This one should work perfectly, just add it to your Country Class or any other place at your code !

/**
* Get a country iso with its Name
*
* @param string $country_name Country Name
* @return string Country iso
*/
static public function getIsoByName($country_name)
{
    $sql='
    SELECT `id_country`
    FROM `'._DB_PREFIX_.'country_lang`
    WHERE `name` = "'.$country_name.'"';
    $result = Db::getInstance()->getRow($sql);
    $iso=Country::getIsoById($result['id_country']);
    return $iso;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM