I need to save the shipping address while order is placed in my custom table how to i take the shipping address . I am using the event sales_order_payment_save_after and did the following codes
$shippingAddress = $order->getShippingAddress();
$city = $shippingAddress->getCity();
$state = $shippingAddress->getRegion();
$countryData = $this->countryInformation->getCountryInfo($shippingAddress->getCountryId());
$country = $countryData->getFullNameLocale();
It is not return any result . how to i get the shipping addess
Solved! Go to Solution.
You need to keep below code in your observer file,
public function execute(Observer $observer) { /** @var OrderPaymentInterface $payment */ $payment = $observer->getDataByKey('payment'); $order = $payment->getOrder(); $shippingAddress = $order->getShippingAddress(); $city = $shippingAddress->getCity(); $state = $shippingAddress->getRegion(); $countryData = $this->countryInformation->getCountryInfo($shippingAddress->getCountryId()); $country = $countryData->getFullNameLocale(); return $this; }
First, you need to get a payment object and based on payment object you need to get Order Data.
You need to keep below code in your observer file,
public function execute(Observer $observer) { /** @var OrderPaymentInterface $payment */ $payment = $observer->getDataByKey('payment'); $order = $payment->getOrder(); $shippingAddress = $order->getShippingAddress(); $city = $shippingAddress->getCity(); $state = $shippingAddress->getRegion(); $countryData = $this->countryInformation->getCountryInfo($shippingAddress->getCountryId()); $country = $countryData->getFullNameLocale(); return $this; }
First, you need to get a payment object and based on payment object you need to get Order Data.