0

I have a custom function that is triggered with event sales_order_place_after, I need to retrieve the shipping address in this function.

I tried $observer->getCustomerAddress(), but it didn't work. How could I get the shipping address?

This is my function

        public function SalesOrderPlaceAfter($observer)
        {

            $orderIds = $observer->getEvent()->getOrder()->getId();   
                if (!empty($orderIds)) 
                {
//This get me the shipping method. it's working.
    $shipping_method = $observer->getEvent()->getOrder()->getShippingMethod(); 

// this is not working, How can I get the shipping address?
    $shipping_address = $observer->getEvent()->getOrder()->getShippingAddress(); 
                }
        }
3
  • Can you add your Observer.php code to the question?
    – JoshCarter
    Commented Aug 31, 2017 at 14:56
  • I've updated the question to include my custom function. Commented Aug 31, 2017 at 15:01
  • @MagentoLearner How to check shipping country before placing order, i mean if customer choose other country one of my products not eligible to place an order?
    – zus
    Commented Oct 11, 2019 at 7:35

2 Answers 2

2

You've loaded the Address object as per Rene's answer, here's how you'd get the fields;

$_shippingAddress = $observer->getEvent()->getOrder()->getShippingAddress();

echo $_shippingAddress->getFirstname();
echo $_shippingAddress->getLastname();
echo $_shippingAddress->getCompany();
echo $_shippingAddress->getStreetFull();
echo $_shippingAddress->getRegion();
echo $_shippingAddress->getCity();
echo $_shippingAddress->getPostcode();
echo $_shippingAddress->getTelephone();
echo $_shippingAddress->getCountryId();
1
$observer->getEvent()->getOrder()->getShippingAddress()

should get you the shipping address.

4
  • I tried your code, it only returned a "Object" Commented Aug 31, 2017 at 14:53
  • 1
    @xxx have you tried iterating through the Object, or directly calling part of it, e.g. $observer->getEvent()->getOrder()->getShippingAddress()->getCountryId()? Commented Aug 31, 2017 at 15:18
  • @RickyOdinMatthews I can get country ID with your code. How can I get the rest address information? Thanks. Commented Aug 31, 2017 at 15:32
  • $observer->getEvent()->getOrder()->getShippingAddress()->getData() will show you what elements the shipping address contains, you can then replace getData() with getCountryId() or other data you want to get. Commented Sep 4, 2017 at 9:55

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.