Question

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 Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;

class RegisterUserOnCoop implements ObserverInterface
{
    /**
    * @var \Magento\Framework\App\ResponseFactory
    */
    protected $_customerRepositoryInterface;

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

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

Any help will be appreciated?

Was it helpful?

Solution

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

namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\Customer;

class RegisterUserOnCoop implements ObserverInterface
{
    /**
    * @var \Magento\Framework\App\ResponseFactory
    */
    protected $_customerRepositoryInterface;

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

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

Thanks!

OTHER TIPS

Try using the code:-

namespace Grazitti\Register\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Customer\Api\CustomerRepositoryInterface;

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top