답변:
블록 파일 생성
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;
}
제공되는 방법 중 하나는 $this->countryFactory->create()->getName();
. 요구 사항에 따라 모델 팩토리를 사용할 수 있습니다.
아래 예에서 사용자 지정 방식으로 pdf를 인쇄해야하는 작업 중 하나에서 청구 지 주소 국가와 배송지 주소 국가가 필요하지만 판매 주문 데이터에서 "SE"와 같은 국가 ID로 가져옵니다. (스웨덴)
메소드에서 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);
}
}
$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();
}
}