1

我正在尝试获取国家名称“美国”(或首字母“美国”/“美国”)。我在以下方面尝试了很多变体:

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();

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

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

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

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

4

3 回答 3

14

通常情况下,国家等信息根本不会存储在客户实体中,我不确定您为什么会得到“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"
于 2011-08-08T23:31:34.560 回答
3

也许这有帮助:

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

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

于 2011-08-08T22:27:46.013 回答
-1

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

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添加到客户实体的属性。我必须通过所有这些爵士乐才能接触到乡村蛞蝓,例如:“美国”。

于 2011-08-09T15:38:36.860 回答