从 Magento 中提取国家名称(或缩写)

发布于 2024-11-28 07:34:52 字数 1071 浏览 1 评论 0原文

我正在尝试获取国家名称“United States”(或缩写“USA”/“US”)。我尝试了以下几种变体:

echo Mage::getSingleton('customer/session')->getCustomer()->getCountry();
$countryName = Mage::getModel('directory/country')->load(Mage::getSingleton('customer/session')->getCustomer()->getCountry())->getName();
error_log("countryName = $countryName");

第一个电话“似乎”有效,因为我得到了 6 回来(美国)。但之后我无法找到如何将其映射到正确的名称或别名。

我看过一些帖子,其中程序员获取了所有国家/地区名称和 ID 的列表,但我不想遍历所有组合来查找名称。

蒂亚!

编辑: 根据 Clockworkgeeks 的建议,我尝试过:

$customer = Mage::getSingleton('customer/session')->getCustomer();
error_log(print_r($customer, true));
$countryCode = $customer->getDefaultBillingAddress()->getCountry();

但是,第三行调用 getCountry() 失败。也许 getDefaultBillingAddress() 没有返回对象。错误消息:

PHP Fatal error:  Call to a member function getCountry() on a non-object in ...

想法? $customer 对象正确返回,尽管 CLASS 是 MYCLIENT_Customer_Model_Customer 对象...也许它没有扩展正确的父对象以使其能够访问 getCountry?

编辑:(解决方案):添加为答案。

I'm trying to get the country name "United States" (or initials "USA"/"US"). I've tried a bunch of variations on the following:

echo Mage::getSingleton('customer/session')->getCustomer()->getCountry();
$countryName = Mage::getModel('directory/country')->load(Mage::getSingleton('customer/session')->getCustomer()->getCountry())->getName();
error_log("countryName = $countryName");

The first call "seems" to work, as I get a 6 back (USA). But after that I'm not able to find out how to map that to a proper name or slug.

I've seen posts where the programmers gets a list of all the country names and id's but I don't want to iterate through all the combinations to find the name.

TIA!

EDIT:
Per clockworkgeeks' suggestion, I've tried:

$customer = Mage::getSingleton('customer/session')->getCustomer();
error_log(print_r($customer, true));
$countryCode = $customer->getDefaultBillingAddress()->getCountry();

However, the 3rd line fails w/the call to getCountry(). Perhaps getDefaultBillingAddress() is not returning on object. Error message:

PHP Fatal error:  Call to a member function getCountry() on a non-object in ...

Thoughts? The $customer object is coming back correctly, though the CLASS is a MYCLIENT_Customer_Model_Customer Object ... perhaps it doens't extend the proper parent to give it access to getCountry?

Edit: (Solution): Added as an answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟花易冷人易散 2024-12-05 07:34:52

通常,像国家/地区这样的信息根本不存储在客户实体中,我不确定为什么你会得到“6”。

$customer = Mage::getSingleton('customer/session')->getCustomer();
$countryCode = $customer->getDefaultBillingAddress()->getCountry();
// $countryCode looks like "US"
$country = Mage::getModel('directory/country')->loadByCode($countryCode);
echo $country->getName(); // looks like "United States"

Normally information like the country is not stored in the customer entity at all, I'm unsure why you get a "6".

$customer = Mage::getSingleton('customer/session')->getCustomer();
$countryCode = $customer->getDefaultBillingAddress()->getCountry();
// $countryCode looks like "US"
$country = Mage::getModel('directory/country')->loadByCode($countryCode);
echo $country->getName(); // looks like "United States"
瑶笙 2024-12-05 07:34:52

也许这有帮助:

$countryName = 
    Mage::getModel('directory/country')->load($countryCode)->getName();

来自:http://blog.chapagain。 com.np/magento-get-country-and-region-collection/

Maybe this helps:

$countryName = 
    Mage::getModel('directory/country')->load($countryCode)->getName();

From: http://blog.chapagain.com.np/magento-get-country-and-region-collection/

陪你到最终 2024-12-05 07:34:52

所以,我通过这样做解决了这个问题:

customer_model_object = Mage::getSingleton('customer/customer');
$session = Mage::getSingleton('customer/session');
$country_name = $customer_model_object->load($session->getId())->getResource()->getAttribute('country')->getFrontend()->getValue($customer_model_object);

感谢您的帮助。你是对的,clockworkgeek,这是从 adminhtml 添加到客户实体的属性。我必须经历所有这些爵士乐才能接触到乡村音乐,例如:“美国”。

So, I solved this by doing:

customer_model_object = Mage::getSingleton('customer/customer');
$session = Mage::getSingleton('customer/session');
$country_name = $customer_model_object->load($session->getId())->getResource()->getAttribute('country')->getFrontend()->getValue($customer_model_object);

Thanks for the help. You were right clockworkgeek, this is an attribute added to the customer entity from the adminhtml. I had to go through all this jazz to get access to the country slug, example: "united states".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文