Good day Mates!
On this weblog, we’re going to learn the way we will get an tackle in string format.
Within the following controller, we have now loaded an tackle by id and received the string format of that tackle.
<?php
/**
* Vendor Desc.
*
* @class Vendor
* @package deal Vendor_CustomModule
* @creator Vendor
* @copyright Copyright (c) Vendor
* @license https://instance.com/license.html
*/
namespace VendorCustomModuleControllerDemo;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
use MagentoFrameworkControllerResultFactory;
class GetAddressFormatString extends Motion
{
/**
* @var ResultFactory
*/
protected $resultFactory;
/**
* initialization
*
* @param Context $context
* @param ResultFactory $resultFactory
* @param MagentoCustomerModelAddressConfig $addressConfig
* @param MagentoCustomerModelAddressMapper $addressMapper
* @param MagentoCustomerApiAddressRepositoryInterface $addressRepository
*
* @return void
*/
public operate __construct(
Context $context,
ResultFactory $resultFactory,
MagentoCustomerModelAddressConfig $addressConfig,
MagentoCustomerModelAddressMapper $addressMapper,
MagentoCustomerApiAddressRepositoryInterface $addressRepository
) {
$this->resultFactory = $resultFactory;
$this->addressRepository = $addressRepository;
$this->addressConfig = $addressConfig;
$this->addressMapper = $addressMapper;
dad or mum::__construct($context);
}
/**
* Execute methodology to get outcome
*/
public operate execute()
{
$addressId = 1;
$outcome = $this->resultFactory->create(MagentoFrameworkControllerResultFactory::TYPE_RAW);
$addressString = $this->getFormattedAddressAsStringByAddressId($addressId);
$result->setHeader('Content material-Kind','textual content/html')
->setContents('Deal with: '.$addressString);
return $outcome;
}
/**
* Get Formatted Deal with as String
*
* @param int $addressId
* @return string
*/
public operate getFormattedAddressAsStringByAddressId($addressId)
{
$addressString = "";
strive {
$addressObject = $this->addressRepository->getById($addressId);
/** @var MagentoCustomerBlockAddressRendererRendererInterface $renderer */
$renderer = $this->addressConfig->getFormatByCode('html')->getRenderer();
$addressString = $renderer->renderArray(
$this->addressMapper->toFlatArray($addressObject)
);
} catch (MagentoFrameworkExceptionNoSuchEntityException $e) {
return "";
}
return $addressString;
}
}
Now, after we execute this controller on the browser. We are going to get the outcome as following picture:
