回答:
ブロックファイルを作成し、
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);
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
指定された国のモデルとその方法を確認します。
/**
*
* @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;
}
それが提供する方法の1つは$this->countryFactory->create()->getName();
です。要件に基づいてモデルファクトリを使用できます。
次の例では、カスタムの方法でPDFを印刷する必要があるタスクの1つで、請求先住所の国と配送先住所の国が必要ですが、注文データから「SE」のような国IDとして取得します(スウェーデン)
メソッドでは、英語またはローカルで、メソッドgetCountryName()に2つの方法で値を設定できます。
ここでは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);
}
}
$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();
}
}