Magento 2 create a customer programmatically

0
283
Magento 2 create a customer programmatically

In this article, you going to see a discussion regarding Magento 2 create a customer programmatically in a detail and step by step.

If you are looking for this topic then you are at a right place.

Lets start with the topic Magento 2 create a customer programmatically.

To create a customer programmatically it is very simple.

You have to just pass required parameter to customer array.

With the help of the function called createCustomer() function.

Call the above create function using the create PHP block or helper file.

Below is code snippet you have to add in template file for customer array from which you can understand easily.

<?php
$customerInfo =[
    'customer' =>[
        'firstname'    => 'Rocky',
        'email'        => 'Rocky.abc@aaaaaaaa.com', //customer email id
        'lastname'     => 'abc',
        'password' => 'admin123',
        'prefix' => 'Mr',
        'suffix' => ''
    ]
];

$block->createCustomer($customerInfo);
?>

Then in the next step is in block file.

Pass customer required data from above $customerInfo array.

Using customer required data create customer in Magento 2.

To create customer programmatically using customer required data.

Use function called createCustomer() function.

For creating customer you have to use the customer class model also called as factory class.

Below is class path you have to add.

\Magento\Customer\Model\CustomerFactory $customerFactory,

Use the below code snippet to create customer programmatically.

<?php
namespace VendorName\ModuleName\PathtoBlock;

class Customer extends \Magento\Framework\View\Element\Template
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Customer\Model\CustomerFactory $customerFactory,
        array $data = []
    ) {
        $this->storeManager = $storeManager;
        $this->customerFactory = $customerFactory;
        parent::__construct($context, $data);
    }
 
     /** Create customer
      *  Pass customer data as array
      */
    public function createCustomer($data) {
 
        $store = $this->storeManager->getStore();
        $storeId = $store->getStoreId();
        $websiteId = $this->storeManager->getStore()->getWebsiteId();
        $customer = $this->customerFactory->create();
        $customer->setWebsiteId($websiteId);
        $customer->loadByEmail($data['customer']['email']);// load customer by email address
        if(!$customer->getId()){
            //For guest customer create new cusotmer
            $customer->setWebsiteId($websiteId)
                    ->setStore($store)
                    ->setFirstname($data['customer']['firstname'])
                    ->setLastname($data['customer']['lastname'])
                    ->setPrefix($data['customer']['prefix'])
                    ->setEmail($data['customer']['email'])
                    ->setPassword($data['customer']['password']);
            $customer->save();
        }
    }
}

At the end, customer get created programmatically.

After adding the code for creating customer in the block file.

Execute the below command to get out put.

php bin/magento indexer:reindex
php bin/magento cache:flush

For final cross-check, go to Admin panel.

Next step is log in your admin panel with username and password.

Click on Customers Tab -> All Customer

As you will see that new customer is get created.

Magento 2 get product collection by product SKU

Conclusion :

Finally, we have done with the topic completely.

With the help of above code snippet.

One can do operation for Magento 2 create a customer programmatically.

I hope you like this article and if you have any query, please comment below.