【问题标题】:Extracting Country name (or initials) from Magento从 Magento 中提取国家名称(或缩写)
【发布时间】:2011-08-08 22:18:52
【问题描述】:

我正在尝试获取国家名称“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 的列表,但我不想遍历所有组合来查找名称。

TIA!

编辑: 根据 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?

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

【问题讨论】:

    标签: php magento country


    【解决方案1】:

    通常情况下,国家/地区等信息根本不会存储在客户实体中,我不确定您为什么会得到“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"
    

    【讨论】:

    • 这可能是因为 EAV 系统,也可能是我们构建或扩展的某些东西。再说一次,我刚刚被投入到这个项目中,我对 Magento 的总体了解很少/为零。
    • 那失败了,对 getCountry() 的调用不起作用,所以 getDefaultBillingAddress() 可能没有返回对象。在我的 OP 中实现。
    • 这表明您的网站已更改为不使用单独的地址对象,或者您正在检查的客户根本没有任何地址。当我查看数据库中的directory_country 表时,它没有数字字段,因此看不到您应该如何使用“6”值。也许有一个本地扩展(检查 MYCLIENT 文件夹)以您可以复制的方式使用该值。
    • 好了,我现在要深入研究他们的模型。谢谢,等我弄明白了再报告。
    • 已解决,+1 为您提供帮助。
    【解决方案2】:

    也许这会有所帮助:

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

    发件人:http://blog.chapagain.com.np/magento-get-country-and-region-collection/

    【讨论】:

    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-11
      • 1970-01-01
      • 1970-01-01
      • 2011-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-27
      相关资源
      最近更新 更多