onlinecode

Magento 2 create customer Programmatically

Magento 2 create customer Programmatically

In this post we’ll show you the way to Magento 2 create customer Programmatically, you’ll fill packed with info into the registration kind within the backend of Magento 2 store. However, it’s long if you wish to make an enormous variety of the new customers coming back from multiple addresses (city, state/ province, country), and send them to completely different client teams at constant time.

Therefore, the tutorial topic Magento 2 create customer programmatically is given to the developers. they’re going to work directly with the code.

Overview of creating customer programmatically

Run the example code

The following code example is all you need to work, please insert it into the console when you want to Magento 2 create customer Programmatically.

$object_manager = \Magento\Framework\App\ObjectManager::getInstance();

$customer_setup_factory = $object_manager->create('Magento\Customer\Setup\CustomerSetupFactory');

$setup_interface = $object_manager->create('Magento\Framework\Setup\ModuleDataSetupInterface');

$customer_setup = $customer_setup_factory->create(['setup' => $setup_interface]);

$customer_entity = $customer_setup->getEavConfig()->getEntityType('customer');
$attribute_set_id = $customer_entity->getDefaultAttributeSetId();

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

/*
*  var $attributeSet AttributeSet 
*/
$attribute_sets = $attribute_set_factory->create();
$attribute_group_id = $attribute_sets->getDefaultGroupId($attribute_set_id);

$customer_setup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'attribute_code', [
	'type' => 'varchar',
	'label' => 'Attribute Title',
	'input' => 'text',
	'required' => false,
	'system' => 0,
	'visible' => true,
	'user_defined' => true,
	'position' => 1000,
	'sort_order' => 1000,	
]);

// attribute set for add attribute 
$attribute_val = $customer_setup->getEavConfig()->getAttribute(\Magento\Customer\Model\Customer::ENTITY, 'attribute_code')
->addData([
	'attribute_group_id' => $attribute_group_id,
	'used_in_forms' => ['adminhtml_customer'],
	'attribute_set_id' => $attribute_set_id,
]);

// save attribute value
$attribute_val->save();

conclusion for Magento 2 Create Customer Programmatically

With the given directions of the codes, i feel that you just can feel softer and time-saving to make the new client programmatically. Especially, if you wish to feature many purchasers, you’ll be able to apply the a part of the code during a loop, and complete all quickly.

You also like Get Order Details using Order and customer registration programmatically and Magento2 admin login user detail and Magento 2 Featured products

Exit mobile version