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.

6 REPLIES

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.

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.

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.

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.