I am trying to create an observer in which when admin will change the group of a customer, then customer will recieve a notification via mail that his/her group has been changed.

For this I will have to find both old group id and current group id of customer but I am unable to find old group id. It was working in Magento version 1.x but not in 2.x

I have written following code in changeGroup.php

   namespace Arshad\NotifyOnGroupChange\Observer;
   use Magento\Framework\Event\ObserverInterface;

   class changeGroup implements ObserverInterface
     protected $_objectManager;

     public function __construct(
     \Magento\Framework\ObjectManagerInterface $objectManager
         $this->_objectManager = $objectManager;

     public function execute(\Magento\Framework\Event\Observer $observer)
        $event = $observer->getEvent();
        $customer = $event->getCustomer();
        $customer = $observer->getEvent()->getCustomer();
        $old_id = $customer->getOrigData('group_id');
        echo $old_id;

My events.xml file

      <?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_save_after">
             <observer name="Arshad_NotifyOnGroupChange_Customer_Save_After" instance="Arshad\NotifyOnGroupChange\Observer\changeGroup" />

3 Answers 3


if you will take a look at how magento save customer then you will come to know that you will not get any old data in "customer_save_after" event. so better you can use "customer_prepare_save" here you will get old group and from request object you can get new group so your work will be easy. following code is to show in "customer_prepare_save" event magento passes two object one is for customer and second is for request. in customer you will get old group and in request you can get new group.

                    ['customer' => $customer, 'request' => $request]

This code is from Magento\Customer\Controller\Adminhtml\Index\Save approx line number 233.

Hope it helps Thanks.

  • How to pull data from here in my controller file? Commented Mar 17, 2017 at 16:54
  • Can you please tell me why you want to pull data to controller ? Commented Mar 20, 2017 at 5:20
  • Sorry but not controller. I want to pull in observer. What I want is that when admin changes customer's group then an observer will be called where I want to pull old group id and new group id of customer. Commented Mar 20, 2017 at 7:06
  • Yes I understood I already give proper solution you just need to dig bit in Magento\Customer\Controller\Adminhtml\Index\Save file and you can do this easlily. Just check in this file how they are getting post data (which will be new group of customer) Commented Mar 20, 2017 at 9:51

I use the quote object for this. For example:

$quote = $this->checkoutSession->getQuote();
$customerGroupId = $quote->getCustomerGroupId();

It is also possible with customerFactory :

$customerFactory = $this->customerFactory->create()->load($quote->getCustomerId());
$customerData = $customerFactory->getData();

Same here.

$customer->getOrigData() returns null on observer. see github issue here and here

I use this code to get old group and new group :

    public function __construct(
     \Magento\Framework\ObjectManagerInterface $objectManager
     \Magento\Framework\App\RequestInterface $request,
        $this->_objectManager = $objectManager;
        $this->_request = $request;

    public function execute(\Magento\Framework\Event\Observer $observer)
        // New data
        $requestData = $this->_request->getPost();

        // Old data
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerLoaded = $objectManager->create('Magento\Customer\Model\Customer')->load($requestData['customer']['entity_id']);

        // Get group id
        $newGroupId = $requestData['customer']['group_id'];
        $oldGroupId = $customerLoaded->getGroupId();


Hope that helps.

  • 1
    Retrieving the Object Manager via static access and utilizing the Object Manager to instantiate a Model as described here is poor practice. Also, usage of $model->load is deprecated in favor of Repository implementation. Models should be new'd up via Factories and persistent data should be retrieved from the Repository. Both the Factory and Repository should be injected into the consuming class via Magneto's DI implementation.
    – John Hall
    Commented Aug 30, 2018 at 16:13

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.