Magento2 create new customer programmtically

  • Create new php file in Magento 2 root folder i.e create_new_customer.php.
  • Include bootstrap.php which contains all the Magento class.
  • Check whether the email address is already registered or not.
  • If the email address already registered, return the error message.
  • If the email address is not registered, set the customer information (Firstname, Lastname, Email and Address) and create the new customer account.
<?php
use Magento\Framework\App\Bootstrap;

require __DIR__ . '/app/bootstrap.php';
$params = $_SERVER;
$bootstrap = Bootstrap::create(BP, $params);
$obj = $bootstrap->getObjectManager();
$state = $obj->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
$websiteId = $storeManager->getStore()->getWebsiteId();
$firstName = 'davis';
$lastName = 'du';
$email = 'davis@du.com';
$password = 'Test@123';
$address = array(
    'customer_address_id' => '',
    'prefix' => '',
    'firstname' => $firstName,
    'middlename' => '',
    'lastname' => $lastName,
    'suffix' => '',
    'company' => '',
    'street' => array(
        '0' => 'Customer Address 1', // this is mandatory
        '1' => 'Customer Address 2' // this is optional
    ),
    'city' => 'New York',
    'country_id' => 'US', // two letters country code
    'region' => 'New York', // can be empty '' if no region
    'region_id' => '43', // can be empty '' if no region_id
    'postcode' => '10450',
    'telephone' => '123-456-7890',
    'fax' => '',
    'save_in_address_book' => 1
);

$customerFactory = $objectManager->get('\Magento\Customer\Model\CustomerFactory')->create();
/**
 * check whether the email address is already registered or not
 */
$customer = $customerFactory->setWebsiteId($websiteId)->loadByEmail($email);
/**
 * if email address already registered, return the error message
 * else, create new customer account
 */
if ($customer->getId()) {
    echo 'Customer with email ' . $email . ' is already registered.';
} else {
    try {
        $customer = $objectManager->get('\Magento\Customer\Model\CustomerFactory')->create();
        $customer->setWebsiteId($websiteId);
        $customer->setEmail($email);
        $customer->setFirstname($firstName);
        $customer->setLastname($lastName);
        $customer->setPassword($password);
        // save customer

        $customer->save();
        $customer->setConfirmation(null);
        $customer->save();
        $customAddress = $objectManager->get('\Magento\Customer\Model\AddressFactory')->create();
        $customAddress->setData($address)
            ->setCustomerId($customer->getId())
            ->setIsDefaultBilling('1')
            ->setIsDefaultShipping('1')
            ->setSaveInAddressBook('1');
        // save customer address

        $customAddress->save();
        echo 'Customer with email ' . $email . ' is successfully created.';
    } catch (Exception $e) {
        echo $e->getMessage();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值