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?
Best Answer
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: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 theMagento\Quote\Model\Quote\Address\ToOrderAddress
model and convert your quote address to the order address:where the
$this->quoteToOrderAddressConverter
instance ofMagento\Quote\Model\Quote\Address\ToOrderAddress
. Add it in using dependency injection (prefered) or using theObjectManager
.