1

I am creating customer with repositoryInterface

below is my code

 <?php
 namespace [Vendor]\[Module]\Model;
 use [Vendor]\[Module]\Api\CustomCharityInterface;
 use Magento\Store\Model\StoreManagerInterface;
 use Magento\Customer\Api\AccountManagementInterface;

class CreateCustomer implements CustomCharityInterface
{   
protected $_storeManager;
protected $customerRepository;
protected $accountManagement;
protected $customerFactory;
protected $addressFactory;
protected $customerInterfaceFactory;
protected $encryptInterface;

public function __construct(
    \Magento\Framework\ObjectManagerInterface $objectManager,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
    AccountManagementInterface $accountManagement,
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
    \Magento\Customer\Model\CustomerFactory $customerFactory,
    \Magento\Customer\Model\AddressFactory $addressFactory,
    \Magento\Customer\Api\Data\CustomerInterfaceFactory $customerInterfaceFactory,
    \Magento\Framework\Encryption\EncryptorInterface $encryptInterface

) {
    $this->_objectManager = $objectManager;
    $this->_storeManager = $storeManager;
    $this->accountManagement = $accountManagement;
    $this->customerRepository = $customerRepository;
    $this->customerFactory  = $customerFactory;
    $this->addressFactory  = $addressFactory;
    $this->customerInterfaceFactory  = $customerInterfaceFactory;
    $this->encryptorInterface  = $encryptInterface;
}


/**
 * {@inheritdoc}
 */
public function createCustomer($params) {

    // Get Website ID
    $websiteId  = $this->_storeManager->getWebsite()->getWebsiteId();
    $customer = null;
     try{
        $customer = $this->customerInterfaceFactory->create();
        $customer->setWebsiteId($websiteId);

        $email = "[email protected]";
        $customer->setEmail($email);
        $customer->setFirstname('test');
        $customer->setLastname("test");
        $password = "1234567";
        $hashedPassword = $this->encryptorInterface->getHash($password, true);

        $customer->setCustomAttribute("position","Manager");

        $this->customerRepository->save($customer, $hashedPassword);

     }
     catch(\Exception $e)
     {
            print_r($e->getMessage());
     }

}

}

After using above code customer is not getting created, Please anyone help me in this code

4
  • its working fine to create customer. please check with reindexing Sep 5, 2018 at 8:03
  • for me its not working, i can't see customer created with that email, i checked in database also, its not creating Sep 5, 2018 at 8:04
  • might be you have issue in implements CustomCharityInterface because i did use same code in controller using extends \Magento\Framework\App\Action\Action it works fine Sep 5, 2018 at 8:09
  • How it can be used here please? Sep 5, 2018 at 8:19

2 Answers 2

1

try this,

$customer = $objectManager->get('\Magento\Customer\Api\Data\CustomerInterfaceFactory')->create();
                $customer->setWebsiteId($websiteId);
$customer->setEmail($customeremail); 
$customer->setFirstname($firstname);
$customer->setLastname($lastname);
$customer->setData("position","Manager");
$objectManager->get('\Magento\Customer\Api\CustomerRepositoryInterface')->save($customer, $hashedPassword);

Hope it helps.

7
  • I have custom attribute for that , i am using repositoryInterface, how to use same code for custom attributes Sep 5, 2018 at 8:18
  • with setData() it will mapped your value to your customer attribute. You can use it like this. I already did, and it works fine for me.
    – Akash
    Sep 5, 2018 at 8:20
  • How can i create customer object? with custoemreRepositoryInterface? please update me that line Sep 5, 2018 at 8:25
  • see my updated answer.
    – Akash
    Sep 5, 2018 at 8:32
  • Same again, its not working for me Sep 5, 2018 at 8:42
1

Changes the below code:

$customer->setCustomAttribute("position","Manager");

to

$extensionAttributes = $customer->getExtensionAttributes(); 
$extensionAttributes->setPosition('Manager');
$customer->setExtensionAttributes($extensionAttributes)
9
  • should take customer object with CustomerInterfaceFactory? Sep 5, 2018 at 10:20
  • How to use save method? please update me clear code Sep 5, 2018 at 12:46
  • I am getting below error after using this Uncaught Error: Call to a member function setPosition() on null Sep 5, 2018 at 13:29
  • CustomerInterfaceFactory no. I guess that you donot create extension_attribute.xml at etc folder
    – Amit Bera
    Sep 5, 2018 at 13:52
  • yes, i am not created any file like that.can you please update me full code, as per my request Sep 5, 2018 at 14:13

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.