如何在prestashop中按国家/地区名称获取国家ISO代码

问题描述 投票:0回答:2

我正在prestashop 1.7中创建我自己的模块,该模块用于支付网关,并在创建结账按钮并选择我的支付网关时工作。

在那些插件中,我需要一些字段用于某些目的,因为我需要用户名,电话,电子邮件和国家iso代码。

我在这些变量中收到用户名,电话和电子邮件:

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

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

我也得到国家名称和国家ID,但我需要的是国家ISO代码。我看到prestashop在他们的数据库中有表格,他们也有ISO代码,但我找不到任何方法或推荐的方式来按国家名称或国家ID获取国家ISO代码。

php prestashop prestashop-1.7
2个回答
0
投票

您可以使用Country类方法public static function getIsoById($idCountry)

所以你可以这样做:

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


0
投票

这个应该完美地工作,只需将它添加到您的国家类或您的代码的任何其他地方!

/**
* 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;
}
© www.soinside.com 2019 - 2024. All rights reserved.