AnswerBun.com

Magento 2: Update customer custom attribute value in observer

Magento Asked by Pankaj Sharma on December 24, 2020

I have created a custom attribute by the help of this link. Now I have to save/update value of this attribute in customer_save_before event observer.

Here is my observer.php code:

namespace VendorModuleObserver;
use MagentoFrameworkEventObserverInterface;

class RegisterUserOnCoop implements ObserverInterface
{
    /**
    * @var MagentoFrameworkAppResponseFactory
    */
    protected $_customerRepositoryInterface;

    public function __construct(
        MagentoCustomerApiCustomerRepositoryInterface $customerRepositoryInterface,
    ) {
        $this->_customerRepositoryInterface = $customerRepositoryInterface;
    }

    public function execute(MagentoFrameworkEventObserver $observer)
    {
        $customerData = $observer->getEvent()->getCustomer();
        $customerData->setCustomAttribute('customer_attribute', $value);
        $this->_customerRepositoryInterface->save($customerData);
    }
}

Any help will be appreciated?

2 Answers

Finally, I find out solution for above problem. Posted answer for the helping purpose.

namespace VendorModuleObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoCustomerModelCustomer;

class RegisterUserOnCoop implements ObserverInterface
{
    /**
    * @var MagentoFrameworkAppResponseFactory
    */
    protected $_customerRepositoryInterface;

    public function __construct(
        MagentoCustomerApiCustomerRepositoryInterface $customerRepositoryInterface,
) {
        $this->_customerRepositoryInterface = $customerRepositoryInterface;
    }

    public function execute(MagentoFrameworkEventObserver $observer)
    {   
        $customer = $observer->getEvent()->getCustomer();
        $customerData = $customer->getDataModel();
        $customerData->setCustomAttribute('customer_attribute', $value);
        $customer->updateData($customerData);
        $customer->save();
    }
}

Thanks!

Correct answer by Pankaj Sharma on December 24, 2020

Try using the code:-

namespace GrazittiRegisterObserver;

use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkEventObserver;
use MagentoCustomerApiCustomerRepositoryInterface;

class RegisterUserOnCoop implements ObserverInterface
{
    /** @var CustomerRepositoryInterface */
    protected $customerRepository;

    /**
     * @param CustomerRepositoryInterface $customerRepository
     */
    public function __construct(
        CustomerRepositoryInterface $customerRepository
    ) {
        $this->customerRepository = $customerRepository;
    }

    /**
     * Manages redirect
     */
    public function execute(Observer $observer)
    {
        $accountController = $observer->getAccountController();
        $customer = $observer->getCustomer();
        $request = $accountController->getRequest();
        $customer_number = $request->getParam('customer_attribute');
        $customer->setCustomAttribute('customer_attribute', $coopid[3]);
        $this->customerRepository->save($customer);
    }
}

Hope this resolves your query. Reference taken from https://magento.stackexchange.com/questions/154127/magento-2-save-customer-attribute-value-while-customer-register

I am assuming your event is getting triggered and you are catching the data in this observer.

Answered by Gagan on December 24, 2020

Add your own answers!

Related Questions

Magento 2: Requested store is not found in product

2  Asked on January 3, 2021 by cesarchris710

   

Slow Admin Orders Grid – Magento 2.3.3 Community

0  Asked on January 2, 2021 by alex-heaton

     

Sorting a custom column – Customer Grid

2  Asked on December 31, 2020 by kiwistastegood

       

Mobile-friendly test: Couldn’t load 36 page resources?

1  Asked on December 31, 2020 by vinod-kumar

   

Magento 2 get Admin session in frontend

1  Asked on December 29, 2020 by ramesh

     

Whitespace before SOAP declaration

3  Asked on December 29, 2020 by boruch

     

display magento2 menu based on the customer login

0  Asked on December 29, 2020 by venkata-prasad

       

Override a controller in magento 1.9

0  Asked on December 29, 2020 by mahesh1343

 

Move block after price using layout xml in M1.9

0  Asked on December 27, 2020 by sam-provides

     

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP