0

Getting error

[2021-09-06 16:23:17] report.ERROR: Call to a member function getTelephone() on null {"exception":"[object] (GraphQL\Error\Error(code: 0): Call to a member function getTelephone() on null at /var/www/magento/vendor/webonyx/graphql-php/src/Error/Error.php:174, Error(code: 0): Call to a member function getTelephone() on null at

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $order = $observer->getEvent()->getOrder();
    $customer = $this->customerRepository->get($order->getCustomerEmail());
    $telephone = $order->getShippingAddress()->getTelephone();
}

any way to fix it ?

2
  • any thoughts on this ?
    – devhs
    Commented Sep 7, 2021 at 5:52
  • This is because $order->getShippingAddress() is not an object and $order->getShippingAddress() is null while it must be an object. Can you please print_r($order->getShippingAddress()->getData()) and see what data you are getting? Commented Sep 7, 2021 at 9:39

1 Answer 1

2

Try this

$telephone = $order->getShippingAddress() ? $order->getShippingAddress()->getTelephone() : false;

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.