cancel
Showing results for 
Search instead for 
Did you mean: 

how to get the mobile number from customerFactory

SOLVED

how to get the mobile number from customerFactory

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



1 ACCEPTED SOLUTION

Accepted Solutions

Re: how to get the mobile number from customerFactory

@sanjaymakw0653 

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) {
        //
    }
?>
Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.

View solution in original post

1 REPLY 1

Re: how to get the mobile number from customerFactory

@sanjaymakw0653 

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) {
        //
    }
?>
Problem solved? Click Kudos and "Accept as Solution".
200+ Magento 2 Extensions for Enhanced Shopping Experience.