Skip to main content
added 1203 characters in body
Source Link
Shoaib Munir
  • 9.6k
  • 10
  • 53
  • 108

I have verified your code,You need to first create event.xml file for your are doing it correctly, I think instead of gettingobserver group_idVendor\Module\etc\frontend\events.xml from

<?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 $this->_request->getParam('group_id');Vendor\Module\Observer\ChangeCustomerGroupId.php try this:

<?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->customerRepository>_customerRepositoryInterface->save($customer);;;
        }
    }
}

I have verified your code, your are doing it correctly, I think instead of getting group_id from $this->_request->getParam('group_id'); try this:

$postData = $this->_request->getPost();
$customer->setGroupId($postData["group_id"]);
$this->customerRepository->save($customer);

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);;
        }
    }
}
Source Link
Shoaib Munir
  • 9.6k
  • 10
  • 53
  • 108

I have verified your code, your are doing it correctly, I think instead of getting group_id from $this->_request->getParam('group_id'); try this:

$postData = $this->_request->getPost();
$customer->setGroupId($postData["group_id"]);
$this->customerRepository->save($customer);