Magento 2 Error – Fix Call to getTelephone() on Null

event-observermagento2

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 ?

Best Answer

Try this

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