0

I have an observer for every order status change, and another for every new order. In both observers I need to retrieve the customer's phone number. In order-status-change this happens as follows:

$phone = $order->getBillingAddress()->getTelephone();

However in new-order, none of the below succeed. They all return null. Why does this happen? What could I do?

$order->getCustomerPhone();
$order->getBillingAddress();
$order->getShippingAddress();
$order->getPrimaryBillingAddress();
6
  • You can try to observer the sale orders address save event. Aug 16, 2017 at 14:52
  • can you please tell me which event you are using for order status change? Aug 16, 2017 at 14:52
  • Sure. It's sales_order_save_after for both of them and I distinguish the case according to the path Aug 16, 2017 at 14:53
  • @SiarheyUchukhlebau you mean that my event occurs before the phone is saved on the address? Which exact event are you referring? I can't find any event named sales_order_address_save Aug 16, 2017 at 14:54
  • its a customer_address_save_after Aug 16, 2017 at 15:01

2 Answers 2

0

You can use the event <event name="sales_order_address_save_after"> in which order id already stored (if you need it). Just check is it billing or shipping address:

$event = $observer->getEvent();
/** @see \Magento\Framework\Model\AbstractModel::_getEventData() */
/** @var \Magento\Sales\Model\Order\Address $address */
$address = $event->getDataObject();
/** @var int $orderId */
$orderId = $address->getParentId();
$addressType = $address->getAddressType();

switch ($addressType) {
    case Address::TYPE_BILLING:
        // Do something with billing address
        break;
    case Address::TYPE_SHIPPING:
        // Do something with shipping address
        break;
    default:
        /**
         * This is a not usual case, when address type is not in one of this types: billing or shipping
         */
        break;
}

this event is triggered each time order address was saved. So you should add an additional check: is your info (telephone) was already stored recently or not?

2
  • When exactly is this triggered? Only on orders from frontend? What about when a customer makes an order without saving a new address? I also need the event in case of admin panel's order Aug 16, 2017 at 15:53
  • @IriniKoutaki as you can see it's related with a sales_order_address, not with a customers address, so it will work in any case when an order was placed, both on frontend and backend. PS: sales_order_address is a different entity saved with an order each time to store a customers data on the moment when the order was placed. Aug 16, 2017 at 18:25
0

I found a trick that worked. (The event remained unchanged: sales_order_save_after)

private function getPhoneFromObserver(Observer $observer)
{
    $data = $observer->getEvent()->getOrder();
    foreach( $data['addresses'] as $addr ) {
        if( $addr['telephone'] !== null ) {
            return $addr['telephone'];
        }
    }
    return null;
}

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.