magento 2 change customer group programmatically
<?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);;
}
}
}