Question

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.

Was it helpful?

Solution

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 = 'johndoe10@example.com';
$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

OTHER TIPS

This code for creating customers in Magento 2:

$data = array(
    'website_id' => 0,
    'email' => 'sample@gmail.com',
    '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!

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top