1

trying to get the order shipping address Magento 2. I did the below code but not its not getting.

$order = $observer->getEvent()->getOrder();
// order id i am getting correct. 
$order_id = $order->getId();
$order->getShippingAddress();

Even tried with by load that order using order but still not getting

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $orderObj = $objectManager->get('Magento\Sales\Model\Order');
 $order_info= $orderObj->load($order_id);
 $shippingaddress = $order_info->getShippingAddress());
 $this->logger->info($shippingaddress);
  • Which event you tried to catch? – Khoa TruongDinh Nov 2 '16 at 15:41
  • sales_order_place_after – Dattatray Yadav Nov 2 '16 at 15:41
  • provide output for var_dump($shippingAddress = $order->getShippingAddress();) – Gopal Patel Nov 2 '16 at 15:47
  • @DattatrayYadav still not getting the shipping address? – Khoa TruongDinh Nov 2 '16 at 16:07
  • How about your issue? – Khoa TruongDinh Nov 12 '16 at 5:34
1

If our event xml is correct, our code lines should work:

app/code/Vendor/Module/etc/events.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_place_after">
        <observer name="order_info" instance="Vendor\Module\Observer\OrderInfo" />
    </event>
</config>

app/code/Vendor/Module/Observer/OrderInfo.php

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class OrderInfo implements ObserverInterface
{

    public function execute(Observer $observer)
    {

            /** @var \Magento\Sales\Model\Order $order */
            $order = $observer->getEvent()->getOrder();
            $shippingAddress = $order->getShippingAddress();
            //var_dump($shippingAddress);

    }
}

Should be clear your Magento Cache also.

  • @DattatrayYadav if it works fine, please, mark this answer as accepted. That will help our community. – Khoa TruongDinh Nov 3 '16 at 6:02
  • 1
    its works fine for me. thank you for help. accepted the ans – Dattatray Yadav Nov 15 '16 at 11:17
  • @DattatrayYadav that's great! – Khoa TruongDinh Nov 15 '16 at 13:18
0

I had same problem. This code doesn't work:

$observer->getEvent()->getOrder();

Please try this:

$orderids = $observer->getEvent()->getOrderIds();
foreach($orderids as $oid){
    $orderID = $oid;
}

Then load by order Id.

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.