One of my clients that has several admin users was wanting to add a little bit of accountability to their Magento store when products get deleted.
He is pretty tech savvy and by the time he reached out had already done some research into it. We spent a little bit of time together going through the database and server logs to see if we could piece together who had deleted a specific product, but unfortunately since they have several employees that all login from the same IP address and also use similar browsers, it wasn’t easy to tell who had actually deleted the product. We could find a little bit of information in the index_event table, not enough to tell which user had actually deleted it or what the product name was without also cross checking against a database backup.
So, going forward, we decided to add some logging to the delete process whenever a product is deleted in Magento. I created an observer for the catalog_product_delete_before action, although catalog_product_delete_after would probably work too.
The basic code is below. It assumes you know how to create a Magento 1.X plugin, so only includes the relevant bits needed to hook into the catalog_product_delete_before observer.
In /app/local/NAMESPACE/MODULE/etc/config.xml:
(Obviously, you will need to change ‘NAMESPACE’ and ‘MODULE’ below to the correct namespace/module name)
<adminhtml> <events> <catalog_product_delete_before> <observers> <NAMESPACE_catalog_product_delete_before> <type>singleton</type> <class>NAMESPACE_MODULE/observer_delete</class> <method>catalog_product_delete_before</method> </NAMESPACE_catalog_product_delete_before> </observers> </catalog_product_delete_before> </events> </adminhtml>
In /app/local/NAMESPACE/MODULE/Model/Observer/Delete.php:
class NAMESPACE_MODULE_Model_Observer_Delete{ public function catalog_product_delete_before(Varien_Event_Observer $observer){ $product_data = null; try{ $product_data = $observer->getEvent()->getProduct(); if(!is_object($product_data) || !method_exists($product_data, 'getId') || $product_data->getId() < 0){ return; } $user = Mage::getSingleton('admin/session'); if(is_object($user)){ $delete_info = "{$user->getUser()->getUsername()} ({$user->getUser()->getEmail()}) just deleted product #{$product_data->getId()}. Product Name: {$product_data->getName()}, Product Sku: {$product_data->getSku()}, IP: {$_SERVER['REMOTE_ADDR']}, User Agent: ".htmlspecialchars($_SERVER['HTTP_USER_AGENT']); } else{ $delete_info = "Unkown User just deleted product #{$product_data->getId()}. Product Name: {$product_data->getName()}, Product Sku: {$product_data->getSku()}, IP: {$_SERVER['REMOTE_ADDR']}, User Agent: ".htmlspecialchars($_SERVER['HTTP_USER_AGENT']); } Mage::log($delete_info, null, 'deleted_product.log', true); } catch(Exception $e){ $delete_info = "Exception deleting product #{$product_data->getId()}. Product Name: {$product_data->getName()}, Product Sku: {$product_data->getSku()}, IP: {$_SERVER['REMOTE_ADDR']}, User Agent: ".htmlspecialchars($_SERVER['HTTP_USER_AGENT']).", Exception: {$e->getMessage()}"; Mage::log($delete_info, null, 'deleted_product.log', true); } } }
Add a Comment