In Magento2.1, I want to make a module to record the product change and changer in database. I think I can use events and observers to achieve it. The catalog_product_save_after
event maybe is a good beginning.
But, when I use the event, how can I know what is the specific attribute the user changed? For example, the price change. How can I know the user changed the price, instead of changing anything else?
Maybe Magento2 has a better event for the specific attribute change? Or a good way to distinguish the price change or anything else? Any suggestion is welcome, thanks 🙂
Best Answer
Event
Use the
catalog_product_save_after
event in theVendor/Model/etc/adminhtml/events.xml
Observer
In the Observer, use the $observer->getProduct()->getOrigData('...') is the point to get specific attribute change.
In the Observer, use the Magento\Backend\Model\Auth\Session to get the changer.
Merge above two Observer parts, now you get the product changes and changer, write them to the DB, then the function is finished.