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('cust123@gmail.com');

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

But the name of the customer doesn't change

Manish
  • 3,106
  • 7
  • 31
  • 47
Me7z
  • 109
  • 1
  • 3
  • 5

2 Answers2

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('cust123@gmail.com', 1);
// Update
$customer->setFirstname('JokeAccount');
$this->_customerRepoInterface->save($customer);
Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352
  • 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 '16 at 14:15
  • 2
    @Ecoplan what if you try $customer->setCustomAttribute('cusattr123','value'); ? – Raphael at Digital Pianism Sep 27 '16 at 14:17
  • i tried it but it didn't work too – Me7z Sep 27 '16 at 14:24
  • not working bro i did using helper but till not working... – Sarfaraj Sipai Jun 21 '18 at 07: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 '19 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.') );
}
Manish
  • 3,106
  • 7
  • 31
  • 47