Hi
i want to get the mobile number after the any used login.
I build the customer Magento modules.
here is model inside
<?php namespace Sanjay\welcome\Model; use Magento\Customer\Model\CustomerFactory; use Magento\Store\Model\StoreManagerInterface; use Psr\Log\LoggerInterface; class Customer { /** * @var CustomerFactory */ protected $customerFactory; /** * @var StoreManagerInterface */ protected $storeManager; /** * @var LoggerInterface */ private $logger; /** * Customer constructor. * @param CustomerFactory $customerFactory * @param StoreManagerInterface $storeManager * @param LoggerInterface $logger */ public function __construct( CustomerFactory $customerFactory, StoreManagerInterface $storeManager, LoggerInterface $logger ) { $this->customerFactory = $customerFactory; $this->storeManager = $storeManager; $this->logger = $logger; } /** * @param $email * @return mixed */ public function getCustomer($email) { try { $websiteId = $this->storeManager->getStore()->getWebsiteId(); $customer = $this->customerFactory->create(); $customer->setWebsiteId($websiteId); $customer->loadByEmail($email); return $customer; } catch (\Exception $e) { $this->logger->error('Something went wrong with customer' . $e->getMessage()); } } }
now i am access this the method
// email i am getting correct // i am able to get the user name city etc . data but facing the issue with mobile number $customerData = $this->_customer->getCustomer($email); $customerData->getTelephone(); // i am getting null herer
Solved! Go to Solution.
You can use the below code:
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); /* Create a new product object */ $customer = $objectManager->create(\Magento\Customer\Model\Customer::class); $Address = $objectManager->get(\Magento\Customer\Api\AddressRepositoryInterface::class); $customer->setWebsiteId("Website ID"); $customer->loadByEmail("Your email"); $billingAddressId = $customer->getDefaultBilling(); $shippingAddressId = $customer->getDefaultShipping(); //get default billing address try { $billingAddress = $Address->getById($billingAddressId); $telephone = $billingAddress->getTelephone(); echo $telephone; } catch (\Exception $e) { // } ?>
You can use the below code:
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); /* Create a new product object */ $customer = $objectManager->create(\Magento\Customer\Model\Customer::class); $Address = $objectManager->get(\Magento\Customer\Api\AddressRepositoryInterface::class); $customer->setWebsiteId("Website ID"); $customer->loadByEmail("Your email"); $billingAddressId = $customer->getDefaultBilling(); $shippingAddressId = $customer->getDefaultShipping(); //get default billing address try { $billingAddress = $Address->getById($billingAddressId); $telephone = $billingAddress->getTelephone(); echo $telephone; } catch (\Exception $e) { // } ?>