У наведеному нижче прикладі в одному із завдань мені потрібно роздрукувати pdf спеціальним способом, тому мені потрібна країна адреси рахунку та країна адреси доставки, але з даних замовлення на продаж я отримую його як ідентифікатор країни, наприклад, "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);
}
}