4

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

  • print get_class_methods($order). You will get number of methods to fetch data. – Sameer Bhayani Aug 4 '17 at 10:34
7

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
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() ?>
  • still get same issue.blank page. – vishnu salunke Aug 4 '17 at 9:34
2

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);        
    $billingAddress = $this->order->getBillingAddress();
    $shippingAddress = $this->order->getShippingAddress();
}

Hope it will help you.

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

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.