Magento – Magento 2: How to get product old and new data on product save button in admin backend

backendevent-observermagento2productsave

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/di.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.

Best Answer

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
            }
        }
Related Topic