4

I was trying to let the customer choose the customer group while the customer registration in magento 2.

What i did was, i passed the group id on hidden field like:

<input type="hidden" name="group_id" id="group_id" value="2" />

But it is not working. When i check the customer from admin panel, the default group was chosen.

My question is:

  1. Can I change the customer group this way?
  2. I can see another method of using event observer method on customer register success but I would prefer this method if it works?

Can anyone help on this?

Thanks

3
  • 1
    The solution you are suggesting is for magento 1 but i am asking for magento 2. Please read the question carefully. Commented Oct 20, 2016 at 5:20
  • try : $this->session->setCustomerGroupId("2"); Commented Oct 20, 2016 at 5:26
  • Where should i add above snippet?? Commented Oct 20, 2016 at 5:46

3 Answers 3

12

I get to solve it so i thought answering here so that it would help others too.

What I did?

I choose event-observer approach to get this done. I created a module which track the customer register success event.You can create the module on the fly from https://mage2gen.com/ . This is very cool and it let's to create a module easily. I just created an observer with the event customer_register_success and it gives me the whole module.

It provide with the observer, with just class and few things defined.

Below is the code added by me:

<?php 


namespace Custom\Customergroup\Observer\Customer;
// added by me
use Magento\Customer\Api\CustomerRepositoryInterface;




class RegisterSuccess implements \Magento\Framework\Event\ObserverInterface {

/**
     * @var CustomerRepositoryInterface
     */
//added by me
    private $customerRepository;

    public function __construct(
        \Magento\Framework\App\RequestInterface $request,
         CustomerRepositoryInterface $customerRepository
        )
    {
        $this->_request = $request;
        $this->customerRepository = $customerRepository;
    }
    //ends here
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ){      

        $id = $observer->getEvent()->getCustomer()->getId();
        $customer = $this->customerRepository->getById($id);

         $group_id = $this->_request->getParam('group_id');

        $customer->setGroupId($group_id);

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

    }
}

I have added the comment which shows my modification.

If anyone has better solution please feel free to add.

Regards

6
  • have you added your code in that module in observer folder Commented Feb 21, 2017 at 7:48
  • can you tell me in brief with path of your extension Commented Feb 21, 2017 at 7:48
  • check the name space to get the path of the file name Commented Feb 22, 2017 at 10:13
  • 1
    I have tested it and looks like all fine only the customer load because it is giving error that no customer exists with the id, So one thing I assume is you already have customer data from which you are getting id, So there is no need to load the customer again. But +10 for the great idea of creating extension on the fly, it helps me a lot. Thank you
    – Asish Hira
    Commented Jul 31, 2017 at 21:52
  • Sorry for the selfless promotion but there is ready made extension that does this and much more - bit.ly/M2-Customer-Group-Switcher-Pro-Fb
    – MagePsycho
    Commented Oct 3, 2017 at 19:44
3

First create your configuration file in [Company Name][Module Name]\etc\frontend\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 instance="[Company Name]\[Module Name]\Observer\Frontend\Customer\RegisterSuccess" name="[Company Name]_[Module Name]_observer_frontend_customer_registersuccess_customer_register_success"/>
    </event>
</config>

Then create the observer:

<?php


namespace [Company Name]\[Module Name]\Observer\Frontend\Customer;

use Magento\Framework\Event\ObserverInterface;


class RegisterSuccess implements \Magento\Framework\Event\ObserverInterface
{

     const CUSTOMER_GROUP_ID = 2;

      protected $_customerRepositoryInterface;

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

    /**
     * Execute observer
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
       $customer = $observer->getEvent()->getCustomer();
        if ($customer->getGroupId() == 1) {
            $customer->setGroupId(self::CUSTOMER_GROUP_ID);
            $this->_customerRepositoryInterface->save($customer);;
        }
    }
}
1
  • working 100% thanks Commented Aug 4, 2020 at 6:44
0

Pls check this link

Magento2 Customer Group Change in a custom registration form #2650

https://github.com/magento/magento2/issues/2650

2
  • Thanks pratik, i had already visited that but it's not much of help. Commented Oct 20, 2016 at 11:42
  • it's working for my end.
    – Pratik
    Commented Oct 21, 2016 at 10:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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