如何从Magento 2中的国家/地区代码中获取国家/地区名称?


10

我想从国家/地区代码中获取国家/地区名称,我从这样的数据顺序中获得国家/地区代码:

$data = $order->getShippingAddress()->getData();
$countryCode = $data['country_id'];
echo $countryCode;

它将打印“美国”或任何其他国家代码,是否有办法从该国家代码获取国家名称?


您是否已检查代码以获取国家名称?
Rakesh Jesadiya

Answers:


31

创建块文件,

   public function __construct(
            \Magento\Directory\Model\CountryFactory $countryFactory
        ) {
            $this->_countryFactory = $countryFactory;
        }

    public function getCountryname($countryCode){    
        $country = $this->_countryFactory->create()->loadByCode($countryCode);
        return $country->getName();
    }

从phtml文件调用,

echo $block->getCountryname($countryCode);

1
正确,除了在$ country = $ this-> _ countryFactory-> create()-> loadByCode($ countryCode)之后缺少分号之外,它应该是$ country = $ this-> _ countryFactory-> create()-> loadByCode($国家代码);
艾瑞克(Eirik)

是否可以从国家/地区名称获取国家/地区代码?
Vindhuja


8

我们可以Magento\Directory\Api\CountryInformationAcquirerInterface用来获取国家信息:

/** @var \Magento\Directory\Api\CountryInformationAcquirerInterface $country */

/** @var \Magento\Directory\Api\Data\CountryInformationInterface $data */
    $data = $country->getCountryInfo($data['country_id']);
    $data->getFullNameEnglish();
    $data->getFullNameLocale();

在此处查看有关返回值的更多信息: Magento\Directory\Api\Data\CountryInformationInterface


嗨,你能告诉我,如何用这个Magento中2得到engilsh国名
向字节

这是完美的。我写了一篇基于该解决方案的文章以了解详细信息blog.equaltrue.com/…这可以为您提供帮助@AskBytes
Shuvankar Paul

0

检查给定的国家模型及其方法:

/**
     * 
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param \Magento\Directory\Model\CountryFactory $countryFactory
     */
    public function __construct(
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, 
        \Magento\Directory\Model\CountryFactory $countryFactory    
    ) {  
        $this->scopeConfig = $scopeConfig;
        $this->countryFactory = $countryFactory;
    }

它提供的一种方法是$this->countryFactory->create()->getName();。您可以根据需要使用模型工厂。


0

在下面的示例中,在一项任务中,我需要以自定义方式打印pdf,这就是为什么我需要帐单地址国家/地区和送货地址国家/地区的原因,但是从销售订单数据中,我将其作为国家ID(例如“ SE”)打开(对于瑞典)

在方法中,可以使用英语或本地方法对getCountryName()方法进行两种方式的赋值。

这里使用CountryInformationAcquirerInterface。

这是完整的代码

namespace Equaltrue\Orderprint\Block\Order;

use Magento\Backend\Block\Template\Context;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Template;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Directory\Api\CountryInformationAcquirerInterface;

class Print extends Template
{
    protected $_coreRegistry;
    protected $orderRepository;
    protected $countryInformationAcquirerInterface;

    /**
     * Constructor
     *
     * @param CountryInformationAcquirerInterface $countryInformationAcquirerInterface
     * @param OrderRepositoryInterface $orderRepository
     * @param Context $context
     * @param Registry $coreRegistry
     * @param array $data
     */
    public function __construct(
        CountryInformationAcquirerInterface $countryInformationAcquirerInterface,
        OrderRepositoryInterface $orderRepository,
        Context $context,
        Registry $coreRegistry,
        array $data = []
    ) {
        $this->orderRepository = $orderRepository;
        $this->countryInformationAcquirerInterface = $countryInformationAcquirerInterface;
        $this->_coreRegistry = $coreRegistry;
        parent::__construct($context, $data);
    }

    /**
     * Retrieve Current Data
     */
    public function getOrderData()
    {
        $orderId = $this->getRequest()->getParam('order_id', 0);
        $order =  $this->getOrder($orderId);

        /*
         * Get billing Address
         * */
        $billingAddress = $order->getBillingAddress();
        $firstNameBilling = $billingAddress->getFirstName();
        $lastNameBilling = $billingAddress->getLastName();
        $streetBilling = implode( ", ", $billingAddress->getStreet());
        $cityBilling = $billingAddress->getCity();
        $postCodeBilling = $billingAddress->getPostCode();
        $countryIdBilling = $billingAddress->getCountryId();
        $countryNameBilling = $this->getCountryName($countryIdBilling);
        $telephoneBilling = "T: ".$billingAddress->getTelephone();
        $formattedBillingAddress = $firstNameBilling." ".$lastNameBilling."<br>". $streetBilling."<br>". $cityBilling.",".$postCodeBilling."<br>".$countryNameBilling."<br>".$telephoneBilling;

        /*
         * Get billing Address
         * */
        $shippingAddress = $order->getShippingAddress();
        $firstNameShipping = $shippingAddress->getFirstName();
        $lastNameShipping = $shippingAddress->getLastName();
        $streetShipping = implode( ", ", $shippingAddress->getStreet());
        $cityShipping = $shippingAddress->getCity();
        $postCodeShipping = $shippingAddress->getPostCode();
        $countryIdShipping = $billingAddress->getCountryId();
        $countryNameShipping = $this->getCountryName($countryIdShipping);
        $telephoneShipping = "T: ".$shippingAddress->getTelephone();
        $formattedShippingAddress = $firstNameShipping." ".$lastNameShipping."<br>". $streetShipping."<br>". $cityShipping.",".$postCodeShipping."<br>".$countryNameShipping."<br>".$telephoneShipping;

        return array(
            "formatted_billing_address" => $formattedBillingAddress,
            "formatted_shipping_address" => $formattedShippingAddress
        );
    }

    /**
     * Getting Country Name
     * @param string $countryCode
     * @param string $type
     *
     * @return null|string
     * */
    public function getCountryName($countryCode, $type="local"){
        $countryName = null;
        try {
            $data = $this->countryInformationAcquirerInterface->getCountryInfo($countryCode);
            if($type == "local"){
                $countryName = $data->getFullNameLocale();
            }else {
                $countryName = $data->getFullNameLocale();
            }
        } catch (NoSuchEntityException $e) {}
        return $countryName;
    }

    protected function getOrder($id)
    {
        return $this->orderRepository->get($id);
    }
}

0

使用objectManager按国家/地区代码获取国家/地区名称。

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $countryCode = 'US'; // Enter country code here
    $country = $objectManager->create('\Magento\Directory\Model\Country')->load($countryCode)->getName();
    echo $country;
?>

谢谢


-3
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
            $allowerdContries = $objectManager->get('Magento\Directory\Model\AllowedCountries')->getAllowedCountries() ;
            $countryFactory = $objectManager->get('\Magento\Directory\Model\CountryFactory');
            //echo "<pre>"; print_r($allowerdContries);

            $countries = array();
            foreach($allowerdContries as $countryCode)
            {
                    if($countryCode)
                    {

                        $data = $countryFactory->create()->loadByCode($countryCode);
                        $countries[$countryCode] =  $data->getName();
                    }
            }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.