0

In Magento2.1, I create a observer use the event catalog_product_save_after, how can I get the user who saved the product in the observer.

The way in Get customer from save event observer Magento 2 doesn't work, because it used different event.

$user = $observer->getCustomer()->getData(); doesn't work too.

0

Use Magento\Backend\Model\Auth\Session in the __construct, then use $this->authSession->getUser() to get the user, it will work. Other solution is welcome to post :)

<?php
namespace Vendor\Module\Observer;

use Magento\Backend\Model\Auth\Session;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class ProductSaveAfter implements ObserverInterface
{
    protected $authSession;

    public function __construct(
        Session $authSession
    )
    {
        $this->authSession = $authSession;
    }

    public function execute(Observer $observer)
    {
        $user = $this->authSession->getUser();
        $userId = $user->getId();
        $userName = $user->getUsername();
        // other codes
    }
}

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.