2

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

 <?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;
        die;
     }
}
?>

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" />
          </event>
        </config>
1

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.

$this->_eventManager->dispatch(
                    'adminhtml_customer_prepare_save',
                    ['customer' => $customer, 'request' => $request]
                );

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

Hope it helps Thanks.

4
  • How to pull data from here in my controller file? – Arshad Hussain Mar 17 '17 at 16:54
  • Can you please tell me why you want to pull data to controller ? – Dhaval Solanki Mar 20 '17 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. – Arshad Hussain Mar 20 '17 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) – Dhaval Solanki Mar 20 '17 at 9:51
0

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();
$customerData['group_id'];
-1

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
  • 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 Aug 30 '18 at 16:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.