2

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

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

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

  • have you added your code in that module in observer folder – Sarvesh Tiwari Feb 21 '17 at 7:48
  • can you tell me in brief with path of your extension – Sarvesh Tiwari Feb 21 '17 at 7:48
  • check the name space to get the path of the file name – aton1004 Feb 22 '17 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 Jul 31 '17 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 Oct 3 '17 at 19:44
0

Pls check this link

Magento2 Customer Group Change in a custom registration form #2650

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

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

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);;
        }
    }
}

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.