Magento – Get Billing and Get Shipping Address in Magento 2

magento2

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..

Best Answer

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": "johndoe@abc.com",
"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
Related Topic