TransWikia.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!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP