Bagaimana cara mendapatkan nama negara dari kode negara di Magento 2?

10

saya ingin mendapatkan nama negara dari kode negara, saya mendapat kode negara dari urutan data seperti ini:

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

itu akan mencetak 'AS' atau kode negara lain, apakah ada cara untuk mendapatkan nama negara dari kode negara ini?

pria sederhana
sumber
Sudahkah Anda memeriksa kode untuk mendapatkan nama negara?
Rakesh Jesadiya

Jawaban:

31

Buat file Blok,

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

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

Panggilan dari file phtml,

echo $block->getCountryname($countryCode);
Rakesh Jesadiya
sumber
1
Benar kecuali Anda kehilangan titik koma setelah $ country = $ this -> _ countryFactory-> create () -> loadByCode ($ countryCode) itu harus $ country = $ this -> _ countryFactory-> create () -> loadByCode ($ Kode negara);
Eirik
Apakah mungkin mendapatkan kode negara dari nama negara?
Vindhuja
@Vindhuja Periksa rakeshjesadiya.com/...
Rakesh Jesadiya
8

Kita dapat menggunakan Magento\Directory\Api\CountryInformationAcquirerInterfaceuntuk mendapatkan info negara:

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

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

Lihat lebih lanjut di sini tentang nilai yang dikembalikan: Magento\Directory\Api\Data\CountryInformationInterface

Khoa TruongDinh
sumber
Hai, Bisakah Anda memberi tahu saya, bagaimana cara menggunakan ini untuk mendapatkan nama negara engilsh di magento 2
Tanyakan Bytes
Ini sempurna. saya menulis artikel berdasarkan solusi ini untuk perincian blog.equaltrue.com/... ini dapat membantu Anda @AskBytes
Shuvankar Paul
0

Periksa model negara yang diberikan dan metodenya:

/**
     * 
     * @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;
    }

Salah satu metode yang disediakannya adalah $this->countryFactory->create()->getName();. Anda dapat menggunakan pabrik model berdasarkan kebutuhan Anda.

Sanjay Chaudhary
sumber
0

Dalam contoh di bawah ini, dalam salah satu tugas saya perlu mencetak pdf dengan cara kustom, itu sebabnya saya memerlukan negara alamat penagihan dan negara alamat pengiriman tetapi, dari data pesanan penjualan saya mendapatkannya sebagai id negara seperti "SE" (untuk Swedia)

dalam metode, Anda bisa memberi nilai dalam dua cara pada metode getCountryName (), dalam bahasa Inggris atau lokal.

CountryInformationAcquirerInterface digunakan di sini.

ini kode lengkapnya

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);
    }
}
Shuvankar Paul
sumber
0

Dapatkan nama negara dengan kode negara menggunakan 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;
?>

Terima kasih

Sohel Khan
sumber
-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();
                    }
            }
Jaykumar R
sumber