0

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.

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

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


         $address = $this->addressDataFactory->create();
         $address->setFirstname($parameters["first-name"])
            ->setLastname($parameters["last-name"])
            ->setCountryId("US")
            //->setRegionId("1")
            //->setRegion("")
            ->setCity('n/a')
            ->setPostcode($parameters["zip"])
            ->setCompany('n/a')
            ->setCustomerId($customerId)
            ->setStreet(['n/a'])
            ->setTelephone($parameters["phone"])
            >setIsDefaultBilling(1)
            ->setIsDefaultShipping(1);

        $this->addressRepository->save($address);

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

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();
                $request->setParams($customerData);
                $customer = $this->customerExtractor->extract('customer_account_create', $request);
                $password = $this->generatePassword();
                $customer = $this->customerAccountManagement->createAccount($customer, $password);
                $this->_customerSession->setCustomerDataAsLoggedIn($customer);
            } else {
                $customer = $this->_customer;
                $customer->setWebsiteId($websiteId);
                $customer->loadByEmail($customerEmail);
                $this->_customerSession->setCustomerAsLoggedIn($customer);
            }
        }
        
        
        public function generatePassword()
        {
            $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
            $randomString = substr(str_shuffle($characters), 0, 10);
            return $randomString;
        }   
    
          
0

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 you have read our privacy policy.

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