从 Magento 中提取国家名称(或缩写)
我正在尝试获取国家名称“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常,像国家/地区这样的信息根本不存储在客户实体中,我不确定为什么你会得到“6”。
Normally information like the country is not stored in the customer entity at all, I'm unsure why you get a "6".
也许这有帮助:
来自:http://blog.chapagain。 com.np/magento-get-country-and-region-collection/
Maybe this helps:
From: http://blog.chapagain.com.np/magento-get-country-and-region-collection/
所以,我通过这样做解决了这个问题:
感谢您的帮助。你是对的,clockworkgeek,这是从 adminhtml 添加到客户实体的属性。我必须经历所有这些爵士乐才能接触到乡村音乐,例如:“美国”。
So, I solved this by doing:
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".