Creating a new customer in Magento 2 can be done by completing the registration form in the backend. However, this process can be quite time consuming, especially if you need to add a large number of customers with multiple addresses (such as city, state/province, and country) and assign them to different customer groups simultaneously.

To address this challenge, this tutorial focuses on how to create customers programmatically in Magento 2. Developers can work directly with the code to streamline the process.

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();

Creating customers programmatically in Magento 2 allows you to efficiently add multiple customers without the need for manual entry through the backend. This method is especially useful for bulk uploads, allowing you to specify details such as addresses and customer groups in a streamlined manner.

In this tutorial, we will guide you through the steps to programmatically create customers using Magento 2’s functionality, ensuring you can manage customer data more effectively and save time.

Following these instructions will help you feel more at ease and save time when creating new customers programmatically. This approach is particularly beneficial if you need to add a large number of customers, as you can incorporate the code into a loop to complete the process quickly.

If you encounter any issues while working through this tutorial, please don't hesitate to reach out for assistance.