5

Here I want a Billing and Shipping addresses according to order_id. I receive a order data in my order object but it will give me just one record according to entity id.I want both address of order(billing and shipping).

Here is my code..

protected $order;

public function __construct(
    \Magento\Sales\Model\Order\Address $order
) {
    $this->order = $order;
  }

public function orderlist($orderid) {
    $order = $this->order->load($orderid);
    $a = $order->getData();
}

when I am printing $a it is giving me particular id record.I tried both getBillingAddress() and getShippingAddress(). it is giving me null value and blank page. I not understanding the actual problem.

can any one help me? thanks in advance..

1
  • print get_class_methods($order). You will get number of methods to fetch data. Commented Aug 4, 2017 at 10:34

3 Answers 3

12

I know, this was late, but just for use of other developers

$shippingaddress = $_order->getShippingAddress()->getData();
print_r($shippingaddress);

It will print all field inside array, then you will get the idea which field to retrive from it for example

echo $_order->getShippingAddress()->getData("postcode");

to get postcode of shipping address

output of print_r($shippingaddress);

"entity_id": "164",
"parent_id": "180",
"customer_address_id": "6",
"quote_address_id": null,
"region_id": "60",
"customer_id": null,
"fax": null,
"region": "Virgin Islands",
"postcode": "111111",
"lastname": "Doe",
"street": "ca\nca",
"city": "ca",
"email": "[email protected]",
"telephone": "1512121212",
"country_id": "US",
"firstname": "John",
"address_type": "shipping",
"prefix": null,
"middlename": null,
"suffix": null,
"company": null,
"vat_id": null,
"vat_is_valid": null,
"vat_request_id": null,
"vat_request_date": null,
"vat_request_success": null
3

You can add this piece of code in the file:

<?php /** @var $_order \Magento\Sales\Model\Order */ ?>
<?php $_order = $block->getOrder() ?>
<?php $billingAddress = $_order->getBillingAddress(); ?>
<?php $shippingAddress = $_order->getShippingAddress(); ?>
With $billingAddress now you can get address e.g:

<?php echo $billingAddress->getStreet() ?>
2
  • still get same issue.blank page. Commented Aug 4, 2017 at 9:34
  • The blank issue is due to getStreet() returns an array
    – devZ
    Commented May 20, 2021 at 9:28
3

Try like below

/** @var \Magento\Sales\Api\Data\OrderInterface $order **/

protected $order;

public function __construct(
    \Magento\Sales\Api\Data\OrderInterface $order,
) {
    $this->order = $order;
  }

public function orderlist($orderid) {
    $order = $this->order->load($orderid);  // pass your order id here       
    $billingAddress = $order->getBillingAddress()->getData();
    $shippingAddress = $order->getShippingAddress()->getData();
}

Hope it will help you.

5
  • i already use it. it will give me a blank page. ad i mention in my question.. Commented Aug 4, 2017 at 8:32
  • I updated my code, please use it and let me know if you have any issues
    – Vino
    Commented Aug 4, 2017 at 9:21
  • You should add $ before before variable shippingAddress should $shippingAddress
    – Aman Alam
    Commented Aug 4, 2017 at 9:23
  • still get same issue.blank page. Commented Aug 4, 2017 at 9:33
  • 1
    This is probably because getShippingAddress() is not defined on the OrderInterface for the API. Commented Sep 29, 2017 at 22:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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