Shopping cart

Magento 2 Create Customer Programmatically

To create a new customer in Magento 2, you can fill full of information into the registration form in the backend of Magento 2 store. However, it is time-consuming if you want to create a huge number of the new customers coming from multiple addresses (city, state/ province, country), and send them to different customer groups at the same time. Therefore, the tutorial topic Magento 2 create customer programmatically is given to the developers. They will work directly with the code.

Overview of creating customer programmatically

Run the code snippet

The following code snippet is all you need to work, please insert it into the console when you want to create customer programmatically.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSetupFactory = $objectManager->create('Magento\Customer\Setup\CustomerSetupFactory');

        $setupInterface = $objectManager->create('Magento\Framework\Setup\ModuleDataSetupInterface');

        $customerSetup = $customerSetupFactory->create(['setup' => $setupInterface]);

        $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
        $attributeSetId = $customerEntity->getDefaultAttributeSetId();

        $attributeSetFactory = $objectManager->create('Magento\Eav\Model\Entity\Attribute\SetFactory');

        /** @var $attributeSet AttributeSet */
        $attributeSet = $attributeSetFactory->create();
        $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'attribute_code', [
            'type' => 'varchar',
            'label' => 'Attribute Title',
            'input' => 'text',
            'required' => false,
            'visible' => true,
            'user_defined' => true,
            'sort_order' => 1000,
            'position' => 1000,
            'system' => 0,
        ]);
        //add attribute to attribute set
        $attribute = $customerSetup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'attribute_code')
        ->addData([
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],
        ]);

        $attribute->save();

Summary

With the given instructions of the codes, I believe that you will feel more comfortable and time-saving to create the new customer programmatically. Especially, if you want to add many customers, you can apply the part of the code in a loop, and complete all quickly.

Related Topics





Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
create
customer
programmatically

Brian Tran
Brian Tran
Senior Magento Developer who is passionate about development and love technology

Comments for Magento 2 Create Customer Programmatically

Please leave comments if you have any questions, feedbacks.

Module Development

We recommend:

You also may like these Magento 2 Extensions

One Step Checkout

$199
120 reviews

Layered Navigation

$99
61 reviews

Gift Card

$199
8 reviews

Reward Points

$99
8 reviews

Affiliate

$149
16 reviews

Shop By Brand

$99
19 reviews

People also searched for:

  • magento 2 customer programmatically
  • create customer programmatically in magento 2
  • create customer group programmatically magento 2
  • magento 2 create new customer programmatically
  • magento 2 create customer attribute programmatically
  • magento 2 create a customer programmatically
  • how to create customer programmatically in magento 2
  • create customer group programmatically in magento 2
  • create admin user programmatically in magento 2
  • create customer programmatically magento 2
  • magento 2 create customer order programmatically