2

I am working on Admin Actions Log Extension. For the reference, I am using the link below

https://amasty.com/admin-actions-log-for-magento-2.html

I want to get old and new data of products while we save product data in admin.

I'm using catalog_product_save_after Observer for the product.

app/code/Vendor/Module/etc/adminhtml/events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
      <event name="catalog_product_save_after">
         <observer name="product_save_after" instance="Vendor\Module\Observer\ProductSaveAfter" />
      </event>
</config>

app/code/Vendor/Module/Observer/ProductSaveAfter.php

<?php

use Magento\Framework\App\RequestInterface;

class ProductSaveAfter implements ObserverInterface
{
    public function __construct(
        \Magento\Catalog\Model\ProductRepository $productRepository
    ) {
        $this->productRepository = $productRepository;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {        
        $product = $observer->getProduct();
        
        // FIRST METHOD
        $compareArray = ['price','special_price','cost','weight','special_from_date','special_to_date','status','visibility','is_salable'];
        
        // $compareArray = [];
        // $id = $product->getId();
        // $productRepo = $this->productRepository->getById($id);
        // $attributes = $productRepo->getAttributes();
        // foreach ($attributes as $attr) {
        //     array_push($compareArray, $attr->getName());
        // }
        foreach ($compareArray as $value) {
            $old = $product->getOrigData($value);
            $new = $product->getData($value);
            if ($old !== $new) {
                // $event .= "CHANGE:- $value $old=>$new";
                $logs = [
                    'old_value' => $old,
                    'new_value' => $new,
                ];
                // $logs is saved to my custom table
            }
        }
    }

}

I'm getting only $compareArray attribute change. I want all getAttributes() attribute changes.

When I use the first method to save the $logs it's working properly but while using the second method and trying to get the difference of $old and $new data somehow it doesn't save to my custom table.

 // SECOND METHOD    
 $compareArray = [];
 $id = $product->getId();
 $productRepo = $this->productRepository->getById($id);
 $attributes = $productRepo->getAttributes();
 foreach ($attributes as $attr) {
      array_push($compareArray, $attr->getName());
 }
 foreach ($compareArray as $value) {
        $old = $product->getOrigData($value);
        $new = $product->getData($value);
        if ($old !== $new) {
            // $event .= "CHANGE:- $value $old=>$new";
            $logs = [
                'old_value' => $old,
                'new_value' => $new,
            ];
        }
    }

If anyone has a solution please let me know.

4
  • I think you can make use of $object->getOrigData(); to get previous data in the plugin. Jul 26, 2019 at 11:26
  • please check updated question
    – Mohit Rane
    Jul 26, 2019 at 11:45
  • How do you know this isn't working? You don't do anything with the $event variable afterwards. You also need to give your observer a better name than product_save_after - they need to be unique and naming it that can cause conflicts. Jul 29, 2019 at 14:23
  • An observer is working fine, I need to get the difference between $old and $new data.
    – Mohit Rane
    Jul 30, 2019 at 5:01

1 Answer 1

1

Try below code to get old and new data:

foreach ($compareArray as $value) {
            $old = $product->getOrigData($value);
            $new = $product->getData($value);
            if ($old !== $new) {
                // $event .= "CHANGE:- $value $old=>$new";
                $logs = [
                    'old_value' => $old,
                    'new_value' => $new,
                ];
                // $logs is saved to my custom table
            }
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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