Magento 2: How to Save Custom Attribute Value After Customer Register

How to Save Custom Attribute Value After Customer Register M2

Hello Magento Folks?,

How are you all going on? Welcome again in our latest article of How To Series for Magento 2 where we are going to understand How to Save Custom Attribute Value After Customer Register Successful in Magento 2. Previously, we have learned about How to Check If Magento 2 is Running In Production Mode with all the required codes. 

Introduction

Basically, Magento 2 is very flexible to use and the development of your own attributes according to your requirements is easily created. Here, in this article, I will explain to you about How to add a custom Attribute and save the value after the customer register Successful with the help of appropriate codes for it. Let’s Get In?.

Steps to Code:

Step 1:

Firstly, create  app\code\Vendor\Extension\view\events.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">

<event name="customer_register_success">

   <observer name="event_customer_register_success" instance="Vendor\Extension\Observer\Customer\RegisterSuccessObserver" />

</event>

</config>

Step 2:

Now create, app\code\Vendor\Extension\ Observer\Customer\RegisterSuccessObserver folder and add this code:

<?php

namespace Vendor\Extension\Observer\Customer;

use Magento\Framework\Event\ObserverInterface;

use Magento\Customer\Api\CustomerRepositoryInterface;

class RegisterSuccessObserver implements ObserverInterface

{

    protected $customerRepository;

    public function __construct(

        CustomerRepositoryInterface $customerRepository)

    {

        $this->customerRepository = $customerRepository;

    }

    public function execute(\Magento\Framework\Event\Observer $observer)

    {

        $customer = $observer->getEvent()->getCustomer();

        $customer->setCustomAttribute('iscustomattribute', 1);

        $this->customerRepository->save($customer);

        return $this;

    }

}

After performing the above step you can easily save your custom attribute on Customer Registration in Magento 2.

Final Words:

I hope the above-given explanation about How to Save Custom Attribute Value After Customer Register Successful in Magento was helpful for all. For more articles like this do comment on your programmatically issues in Magento 2 in the comment section below. If you find any difficulties in performing the above steps then feel free to contact our Support Team. And if you find the blog interesting then do share with your Magento friends and help them. 

Stay Home? Stay Safe! 

Happy Coding 🙂

Magento-development-services

Previous Article

6 Tips to Focus If You Work From Home For Magento Developer

Next Article

Top 10 Ecommerce Facts you need to know in 2024

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨