0

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?

  • It is better if you should share the code here instead of the link. Because the link may expire in future. Also if you paste your code here the helper, can see if there is any error in your code. Hope you got my point. – Gagan Apr 25 '18 at 5:40
  • @LAW Thanks for your suggestion. Updating my question. Meantime, could you please look into this? – Pankaj Sharma Apr 25 '18 at 5:44
2

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!

0

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.

  • Sorry @LAW its not working for me. – Pankaj Sharma Apr 25 '18 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.