3

I want to update customer information programmatically. so I have used below code.

public function execute()
{
    $resultRedirect = $this->resultRedirectFactory->create();
try{
    $path = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('catalog/import/epicore/UPB-customers.csv');
    $dataExcel = fopen($path, "r");
    while (!feof($dataExcel) ) {
        $line_of_text[] = fgetcsv($dataExcel, 1024);
    }
    array_shift($line_of_text); 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    foreach($line_of_text as $customerNewData){
        $websiteId  = $this->storeManager->getWebsite()->getWebsiteId();
        $customer = $objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface')->getById($customerNewData[0]);
        //echo "<pre>"; print_r($customer->getData()); exit;            
        if(count($customer)){
            $data = array(); 
            $email = $customer->getEmail();             
            $customer->setWebsiteId($websiteId);
            $customer->setData("custid", $customerNewData[1]);
            $customer->setData("custnum", $customerNewData[2]);
            $customer->setData("salesforceid", $customerNewData[3]);
            $customer->setEmail($email);                
            $customer->save();
        }else{

        }   
    }           
} catch(\Exception $e) {
        echo $e->getMessage(); exit;
}
$this->_redirect('*/*/');
    return;
}

But Its returns "Please enter email address" error.

3
  • Please check email is getting from customer or not. after that set it in customer. @Ajay Commented Mar 28, 2018 at 4:34
  • it i am checking full object and email address alreadt preset here. @DhadukMitesh
    – Ajay Patel
    Commented Mar 28, 2018 at 4:37
  • Then check you add all require field or not. Commented Mar 28, 2018 at 4:39

1 Answer 1

6

You should use service contracts instead of Model Class (and strictly don't use object manager)

protected $customerRepository;

public function __construct(
    ------------------
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
    -----------------
)
{
    ------------
    $this->customerRepository = $customerRepository;
    -----------
}

public function {myFunction}(){

    $customer = $this->customerRepository->getById($customerNewData[0]);

    if($customer->getId())
    {
    $customer->setWebsiteId($websiteId);
            $customer->setCustid($customerNewData[1]);
            $customer->setCustnum($customerNewData[2]);
            $customer->setSalesforceid($customerNewData[3]);
            $customer->setEmail($email);
    }
    $this->customerRepository->save($customer)  ;
}
6
  • I have already try this but getting save($customer); undefined function.
    – Ajay Patel
    Commented Mar 28, 2018 at 6:56
  • @AjayPatel How could this be undefined. Please double check for any typo or post your class code. Commented Mar 28, 2018 at 10:33
  • Please check my updated question
    – Ajay Patel
    Commented Mar 28, 2018 at 11:11
  • @AjayPatel $customer->save(); is wrong. You need to use $objectManager->create('Magento\Customer\Api\CustomerRepositoryInterface')->save($customer) instead. Commented Mar 28, 2018 at 11:24
  • can you know how to update my custom created attribute and show in magento 2.2 admin panel in customer grid or customer data part?
    – Ajay Patel
    Commented Mar 28, 2018 at 11:31

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.