1

I have a custom module in which I need to get the order details. I am using the below code to get the orders from quotes in admin end,

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$options = $objectManager->create('Magento\Quote\Model\Quote');
$_order = $options->load($_quoteId);

Here I am getting the quote and order details. I am getting the billing address In case I try to print it as below,

print_r($_order->getBillingAddress()->getStreet());
print_r($_order->getBillingAddress()->getFirstName());

However, when I am using the getFormattedAddress() like below, I don't get anything.

echo $block->getFormattedAddress($_order->getBillingAddress());

In my block I have added the getFormattedAddress() function as below,

    public function getFormattedAddress(Address $address) {
    return $this->addressRenderer->format($address, 'html');
}

This gives me an error as,

Uncaught TypeError: Argument 1 passed to Vendor\Module\Block\Adminhtml\Quotes::getFormattedAddress() must be an instance of Magento\Sales\Model\Order\Address, instance of Magento\Quote\Model\Quote\Address given

I tried to go through some of the belwo links but could not manage to get the solution of my problem,

The issue here is, getFormattedAddress() expects an instance of Magento\Sales\Model\Order\Address whereas, I am passing an address of Magento\Quote\Model\Quote\Address.

Is there any way to use getFormattedAddress() using the quote address?

PS: I know the using objectManager directly is not recommended and I have plans to change it once I get the billing and shipping address.

Please, can anyone help?

  • Which class have you used for Address in public function getFormattedAddress(Address $address) – Dinesh Yadav Oct 9 '17 at 6:05
  • I am using \Magento\Sales\Model\Order\Address\Renderer $addressRenderer – manini Oct 9 '17 at 6:21
2

These error comes from a method public function getFormattedAddress(Address $address) because you expect to get the order address as a first argument. Try to remove the argument validation:

public function getFormattedAddress($address) {
    return $this->addressRenderer->format($address, 'html');
}

However if you are using the \Magento\Sales\Model\Order\Address\Renderer class there should be Order Address, so I'll recommend you to use the Magento\Quote\Model\Quote\Address\ToOrderAddress model and convert your quote address to the order address:

public function getFormattedAddress($address) {
    if ($address instanceof \Magento\Quote\Model\Quote\Address) {
        $address = $this->quoteToOrderAddressConverter->convert($address);
    }

    if (!$address instanceof \Magento\Sales\Model\Order\Address) {
        throw new \Exception(__('Expected instance of \Magento\Sales\Model\Order\Address, got ' . get_class($address)));
    }

    return $this->addressRenderer->format($address, 'html');
}

where the $this->quoteToOrderAddressConverter instance of Magento\Quote\Model\Quote\Address\ToOrderAddress. Add it in using dependency injection (prefered) or using the ObjectManager.

1

In Magento\Sales\Model\Order\Address\Renderer Class, first agrument passed with type hint order address object.

use Magento\Sales\Model\Order\Address;
public function format(Address $address, $type)
{

}

So we can not passed quote address object. you can get Address Id from Quote address object and format the address like below:

public function __construct(
    /* add below dependencies In your class construct */
    \Magento\Customer\Api\AddressRepositoryInterface $addressRepository,
    \Magento\Customer\Model\Address\Config $addressConfig,
    \Magento\Customer\Model\Address\Mapper $addressMapper
) {
    $this->addressRepository = $addressRepository;
    $this->_addressConfig = $addressConfig;
    $this->addressMapper = $addressMapper;
}

Add below code in your function to get formatted address:

$addressData = array();
$addressId = $quote->getBillingAddress()->getCustomerAddressId();
try {
    $addressObject = $this->addressRepository->getById($addressId);

    /** @var \Magento\Customer\Block\Address\Renderer\RendererInterface $renderer */
    $renderer = $this->_addressConfig->getFormatByCode('html')->getRenderer();
    $addressData = $renderer->renderArray($this->addressMapper->toFlatArray($addressObject));
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
    echo $e->getMessage();die;
}

echo $addressData;
die;

Hope this help !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.