0

I am trying to get list of all billing addresses of all orders along with other information. Rest is working fine.

This is the function:

 public function getHtml()
   {

        $_orders = $this->order->getOrders();
        foreach ($_orders as $order){
            $itemName = '';
            $itemQty = '';
            $customerName = '';
            $billingAddress = '';
            $OrderId = $order->getEntityId();
            $customerName= $order->getCustomerName();
            $billingAddress = $order->getBillingAddress();
            $OrderItems = $order->getAllItems();
        foreach ($OrderItems as $item) {
          $itemQty.= $item->getQtyOrdered().'<br />';
          $itemName.= $item->getName().'<br />';
        }

                echo $OrderId;
                echo $customerName;
                echo $itemName;
                echo $itemQty
                echo $billingAddress;

}

Problem is when I do

$billingAddress = $order->getBillingAddress();

Get this: 1 exception(s): Exception #0 (Exception): Recoverable Error: Object of class Magento\Sales\Model\Order\Address could not be converted to string in..remote file path and line number

and when

$billingAddress = $order->getBillingAddress()->getStreet();

Get this: 1 exception(s): Exception #0 (Exception): Notice: Array to string conversion in remote file path and line number

Ajwad Syed
  • 1,591
  • 25
  • 61

1 Answers1

0

add below code and excute.let me know if any concern

use Magento\Sales\Model\Order\Address\Renderer;

/**
 * Class Address
 */
class NameClass
{
    protected $addressRenderer;

    public function __construct(
        ................

        Renderer $addressRenderer,

        ................
    ) {
        $this->addressRenderer = $addressRenderer;
    }

     public function getHtml()
     {
        $_orders = $this->order->getOrders();
        foreach ($_orders as $order){
            $itemName = '';
            $itemQty = '';
            $customerName = '';
            $billingAddress = '';
            $OrderId = $order->getEntityId();
            $customerName= $order->getCustomerName();
            $billingAddress = $order->getBillingAddress();
            $billingAddressHtml = $this->addressRenderer->format($billingAddress, 'html');
            $OrderItems = $order->getAllItems();
            foreach ($OrderItems as $item) {
              $itemQty.= $item->getQtyOrdered().'<br />';
              $itemName.= $item->getName().'<br />';
            }

            echo $OrderId;
            echo $customerName;
            echo $itemName;
            echo $itemQty
            echo $billingAddress;

        }
    }

}

OR you can use objectManager also

$objectManager   = \Magento\Framework\App\ObjectManager::getInstance();
$address         = $objectManager->get('Magento\Sales\Model\Order\Address\Renderer');
$billingAddress  = $address->format($billingAddress, 'html');

echo $billingAddress;
Balwant Singh
  • 1,270
  • 2
  • 12
  • 28
  • Removed generation, cache cleared still! Exception #0 (Exception): Recoverable Error: Argument 4 passed to Remote\file\path\CreatePdf\Interceptor::__construct() must be an instance of Magento\Sales\Model\Order\Address\Renderer, none given, called in E:\Xampp\htdocs\magento_15\vendor\magento\framework\ObjectManager\Factory\AbstractFactory.php on line 111 and defined in E:\Xampp\htdocs\magento_15\var\generation\CreteSol\PickList\Controller\Adminhtml\Generatepdf\CreatePdf\Interceptor.php on line 11 – Ajwad Syed Nov 20 '18 at 12:41
  • @AjwadTaqvi try with objectManager .

    $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $address = $objectManager->get('Magento\Sales\Model\Order\Address\Renderer'); $billingAddress = $address->format($billingAddress, 'html');

    echo $billingAddress;

    – Balwant Singh Nov 20 '18 at 12:46
  • Yes it is working with object manager but it is not a recommended way you know. – Ajwad Syed Nov 20 '18 at 12:55
  • @AjwadTaqvi why? who said? just because of objectManger.i don't think so. if it is working for you then please accept the answer. – Balwant Singh Nov 20 '18 at 13:37
  • Yeah Accepted! Here are some reasons for not using objectManagers. Happy learning :) https://magento.stackexchange.com/questions/117098/magento-2-to-use-or-not-to-use-the-objectmanager-directly – Ajwad Syed Nov 20 '18 at 13:48