5

I want to update the firstname of a customer like this :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$customerFactory = $objectManager->create('\Magento\Customer\Model\CustomerFactory');


$customer = $objectManager->create('Magento\Customer\Model\Customer')->setWebsiteId(1)->loadByEmail('[email protected]');

try {
    if(!empty($customer->getData('email')))
    {
        $customer->setFirstname('JokeAccount');
        $customer->save();
    } 
}

But the name of the customer doesn't change

1
  • Isn't your 2nd line conflicting with the 3rd one? Sep 27, 2016 at 14:08

2 Answers 2

8

I'm not entirely sure what's wrong with your code but here's how I did it a few weeks ago using service contracts. Also please try to avoid using the ObjectManager directly

protected $_customerRepoInterface;

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

Then in your code you can do:

// Second parameter is the website id
$customer = $this->_customerRepoInterface->get('[email protected]', 1);
// Update
$customer->setFirstname('JokeAccount');
$this->_customerRepoInterface->save($customer);
5
  • Hey dude this looks fine, my code already worked 50% just didn't mention to reindex... The customers name changed, but i have also some spezial customer field eg: textfield called "cusattr123" i want to fill it with $customer->setCusattr123('value'); but this didn't work.
    – Me7z
    Sep 27, 2016 at 14:15
  • 2
    @Ecoplan what if you try $customer->setCustomAttribute('cusattr123','value'); ? Sep 27, 2016 at 14:17
  • i tried it but it didn't work too
    – Me7z
    Sep 27, 2016 at 14:24
  • not working bro i did using helper but till not working... Jun 21, 2018 at 7:17
  • @RaphaelatDigitalPianism in the above answer I am not able to get that where I am using object manager directly...please explain to me if I am doing anything wrong in detail. Thanks.
    – Manish
    Mar 29, 2019 at 11:13
3

create a controller

   protected $_customerRepositoryInterface;

   public function __construct(
    \Magento\Framework\App\Action\Context $context,
    \Magento\Framework\View\Result\PageFactory $resultPageFactory,
    \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface
  ) {
    parent::__construct($context);
    $this->resultPageFactory = $resultPageFactory;
    $this->_customerRepositoryInterface = customerRepositoryInterface;
  }

Now execute function here reward_barcode is custom attribute:

 public function execute()
  {
     $customerId = 2;
     if ($customerId) {

            $customer = $this->_customerRepositoryInterface->getById($customerId);
            $customer->setCustomAttribute('reward_barcode', $barcode);
            $this->_customerRepositoryInterface->save($customer);

   }
   $this->messageManager->addSuccess( __('Your membership successfully updated.') );
}

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.