1

Hello everyone I want to set group_id = 2 for wholesale customer registration.
I have to override the registration form in view/registration.phtml and also written

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

I have also created the observer

Magneto\WholesaleCustomer\Observer\Customer\RegisterSuccess.php

<?php 


 namespace Magneto\WholesaleCustomer\Observer\Customer;

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

}
}

But the group id is coming 1 in the database I want to set it as 2 in database.

1 Answer 1

1

You need to first create event.xml file for your observer Vendor\Module\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 name="persistent" instance="Vendor\Module\Observer\ChangeCustomerGroupId" />
    </event>
</config>

and create observer after that Vendor\Module\Observer\ChangeCustomerGroupId.php

<?php

namespace Vendor\Module\Observer;

use Magento\Framework\Event\ObserverInterface;

class ChangeCustomerGroupId implements ObserverInterface
{

    protected $_customerRepositoryInterface;
    protected $_request;

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

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $customer = $observer->getEvent()->getCustomer();
        if ($customer->getGroupId() == 1) {
            $postData = $this->_request->getPost();
            $customer->setGroupId($postData["group_id"]);
            $this->_customerRepositoryInterface->save($customer);;
        }
    }
}
14
  • nothing change group id is passing 1 Commented Mar 11, 2019 at 5:34
  • can you share your event.xml code? Commented Mar 11, 2019 at 5:40
  • i does not created event.xml Commented Mar 11, 2019 at 5:41
  • so how would your observer work? Commented Mar 11, 2019 at 5:43
  • can you share me the event.xml code? Commented Mar 11, 2019 at 5:45

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.