I'm stuck in a weird problem.

Everytime I try to create an account and add an address to that account programmatically I get an error called

A customer with the same email already exists in an associated website

This is my code for adding shipping and billing address to customer.

        //$customerId = $customer->getId();
        //Address info

         $customerAddress = $this->customerFactory->create()->setWebsiteId($websiteId)->loadByEmail($customer_email);
         $customerId = $customerAddress->getId();

         $address = $this->addressDataFactory->create();


Of course I'm adding differnt emails everytime I run this controller, but for some reason it keeps telling me that the email is already associated with another customer.

Please help

2 Answers 2


First you have to check customer in website exist or not ?

 public function __construct(\Magento\Customer\Api\AccountManagementInterface $customerAccountManagement,
\Magento\Customer\Model\CustomerExtractor $customerExtractor
        $this->customerAccountManagement = $customerAccountManagement;
        $this->customerExtractor = $customerExtractor;

        public function execute()
            $customerEmail = 'customer email address';
            $websiteId = (int)$this->storeManager->getWebsite()->getId();
            $isEmailNotExists = $this->customerAccountManagement->isEmailAvailable($customerEmail, $websiteId);
            if ($isEmailNotExists == '1') {
                $customerData = ['set your customer Data];
                $request = $this->requestFactory->create();
                $customer = $this->customerExtractor->extract('customer_account_create', $request);
                $password = $this->generatePassword();
                $customer = $this->customerAccountManagement->createAccount($customer, $password);
            } else {
                $customer = $this->_customer;
        public function generatePassword()
            $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $randomString = substr(str_shuffle($characters), 0, 10);
            return $randomString;

Once your forgot this line we got same error.

$customer = $this->accountManagement
                ->createAccount($customer, $password, $redirectUrl);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.