Answers for "magento 2 change customer group programmatically"

0

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);;
        }
    }
}
Posted by: Guest on June-01-2021

Code answers related to "magento 2 change customer group programmatically"

Browse Popular Code Answers by Language