cancel
Showing results for 
Search instead for 
Did you mean: 

Getting both lines of shipping and billing street address after sales_order_place_after

SOLVED

Getting both lines of shipping and billing street address after sales_order_place_after

I have an Observer that runs after the "sales_order_place_after" event is triggered that is used for sending data to another server. This script worked great in Magento 1.9.2.2, but since upgrading to 2.2, we're no longer seeing the individual lines of the street address come through correctly.

We're getting the order details like this:

$order = $observer->getOrder();


...and trying to get the street address like this:

$order->getBillingAddress()->getStreet1()
$order->getShippingAddress()->getStreet1()


and

$order->getBillingAddress()->getStreet2()
$order->getShippingAddress()->getStreet2()


However, it is coming through completely blank. Has something changed in Magento 2.2 that would cause this to not work correctly?

Any assistance is appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Getting both lines of shipping and billing street address after sales_order_place_after

Could you please try with below methods

$billStreet = $order->getBillingAddress()->getStreet()
$shipStreet = $order->getShippingAddress()->getStreet()

You got billing and shipping street array.
Issue solved click kudos/accept as solutions.
If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

View solution in original post

7 REPLIES 7

Re: Getting both lines of shipping and billing street address after sales_order_place_after

Could you please try with below methods

$billStreet = $order->getBillingAddress()->getStreet()
$shipStreet = $order->getShippingAddress()->getStreet()

You got billing and shipping street array.
Issue solved click kudos/accept as solutions.
If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: Getting both lines of shipping and billing street address after sales_order_place_after

Would you then access the individual lines as so:

 

Line 1 would be: $billStreet[0]

 

Line 2 would be: $billStreet[1]

 

Re: Getting both lines of shipping and billing street address after sales_order_place_after

Correct, You can access like above for both shipping and billing address.
If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: Getting both lines of shipping and billing street address after sales_order_place_after

Getting an error during checkout, and the following in exception.log:

 

[2017-11-22 17:50:18] main.CRITICAL: Notice: Undefined offset: 1 in /path-to-magento/app/code/path-to-observer/SendOrderDataObserver.php on line 91 {"exception":"[object] (Exception(code: 0): Notice: Undefined offset: 1 in /path-to-magento/app/code/path-to-observer/SendOrderDataObserver.php on line 91 at /path-to-magento/vendor/magento/framework/App/ErrorHandler.php:61)"} []

 

Line 91 is trying to access line 2 (index 1) of the array:

 

<billToAddress2>' . $billStreet[1] . '</billToAddress2>

Re: Getting both lines of shipping and billing street address after sales_order_place_after

You need to check if line 2 is empty or not and set value like
If(!empty($billStreet[1])){ echo $billShipping[1]}

For both billing and shipping to check conditions.
If issue solved click kudos/accept as solutions.
If Issue Solved, Click Kudos/Accept As solutions. Get Magento insight from
Magento 2 Blogs/Tutorial

Re: Getting both lines of shipping and billing street address after sales_order_place_after

I was actually just coming back to edit that post - I realized it wasn't being checked before accessing it.

 

Thank you for your assistance with this.

Re: Getting both lines of shipping and billing street address after sales_order_place_after

Following up on this issue we had with our observer previously.

 

Attempting to access the street address of the shipping address has caused a fatal error to show up (which, in turn, has caused some orders to be processed but not recorded in Magento).

 

The error is as follows:

 

PHP Fatal error: Uncaught Error: Call to a member function getStreet() on null in /path/to/file/Observer.php:135

 

The line in question is like this:

 

$shipStreet = $order->getShippingAddress()->getStreet();

Here's the issue - if this is a guest order where a user doesn't have an account (so they do not have a shipping address on record), how can we use this functionality to check for an address to be present before actually getting it in our script?