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(); 
  • Can you add your Observer.php code to the question? – JoshCarter Aug 31 '17 at 14:56
  • I've updated the question to include my custom function. – Magento Learner Aug 31 '17 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 Oct 11 '19 at 7:35

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();

should get you the shipping address.

  • I tried your code, it only returned a "Object" – Magento Learner Aug 31 '17 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()? – Ricky Odin Matthews Aug 31 '17 at 15:18
  • @RickyOdinMatthews I can get country ID with your code. How can I get the rest address information? Thanks. – Magento Learner Aug 31 '17 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. – René Schep Sep 4 '17 at 9:55

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.