1

I am trying to log in and register by Facebook and Gmail. So I need to help how to create a user by custom code. There are two kinds of social media login I want to use.

2 Answers 2

4

One of the example of creating customer

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();        
$appState = $objectManager->get('\Magento\Framework\App\State');
//$appState->setAreaCode('frontend'); // not needed if Area code is already set

$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$websiteId = $storeManager->getStore()->getWebsiteId();

$firstName = 'John';
$lastName = 'Doe';
$email = '[email protected]';
$password = 'Test1234';

// instantiate customer object
$customer = $objectManager->get('\Magento\Customer\Model\CustomerFactory')->create();
$customer->setWebsiteId($websiteId);

// if customer is already created, show message
// else create customer
if ($customer->loadByEmail($email)->getId()) {
    echo 'Customer with email '.$email.' is already registered.';  
} else {
    try {        
        // prepare customer data
        $customer->setEmail($email); 
        $customer->setFirstname($firstName);
        $customer->setLastname($lastName);

        // set null to auto-generate password
        $customer->setPassword($password); 

        // set the customer as confirmed
        // this is optional
        // comment out this line if you want to send confirmation email
        // to customer before finalizing his/her account creation
        $customer->setForceConfirmed(true);

        // save data
        $customer->save();

        // save customer address
        // this is optional
        // you can skip saving customer address while creating the customer
        $customerAddress = $objectManager->get('\Magento\Customer\Model\AddressFactory')->create();
        $customerAddress->setCustomerId($customer->getId())
                        ->setFirstname($firstName)
                        ->setLastname($lastName)
                        ->setCountryId('US')
                        ->setRegionId('12') // optional, depends upon Country, e.g. USA
                        ->setRegion('California') // optional, depends upon Country, e.g. USA
                        ->setPostcode('90232')
                        ->setCity('Culver City')
                        ->setTelephone('888-888-8888')
                        ->setFax('999')
                        ->setCompany('XYZ')
                        ->setStreet(array(
                            '0' => 'Your Customer Address 1', // compulsory
                            '1' => 'Your Customer Address 2' // optional
                        )) 
                        ->setIsDefaultBilling('1')
                        ->setIsDefaultShipping('1')
                        ->setSaveInAddressBook('1');

        try {
            // save customer address
            $customerAddress->save();
        } catch (Exception $e) {
            echo 'Cannot save customer address.';
        }                

        // send welcome email to the customer
        $customer->sendNewAccountEmail();

        echo 'Customer with the email ' . $email . ' is successfully created.';

    } catch (Exception $e) {
        echo $e->getMessage();
    }
}

I hope this will help

2
  • Thanks a lot, Sir. Jul 9, 2019 at 5:05
  • 1
    Glad it helps you +1 on your question for appreciation. Jul 9, 2019 at 5:08
4

This code for creating customers in Magento 2:

$data = array(
    'website_id' => 0,
    'email' => '[email protected]',
    'firstname' => 'Mac',
    'lastname' => 'Hamon',
    'group_id' => 1
);
$customer = $this->_objectManager->create('Magento\Customer\Model\Customer');
$customer->addData($data);
$customer->setPasswordHash('something');
$customer->setConfirmation(null);
$customer->save();

Done!

3
  • Thanks a lot, Madam! Jul 9, 2019 at 5:05
  • 1
    @Jamy +1 for your effort and appreciation Jul 9, 2019 at 5:09
  • @MuhammadHasham Thank you!
    – Jamy
    Jul 9, 2019 at 7:17

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.