Skip to main content
added 19 characters in body
Source Link
Gohil Rajesh
  • 3.1k
  • 1
  • 14
  • 25

In this case, you can use the event like:

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

<event name="catalog_product_save_after">
    <observer name="my_custom_product_save_observer" instance="Vendor\module\Observer\Productsaveafter" />
</event>  

And at this observer,you can get identify by below code

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

<?php
    
        namespace Vendor\Module\Observer;
    
        use Magento\Framework\Event\ObserverInterface;
    
        class Productsaveafter implements ObserverInterface
        {
           public function execute(\Magento\Framework\Event\Observer $observer)
            {
                 /** @var Product $product */
                $product = $observer->getEvent()->getProduct();
                if($product->isObjectNew() )
                {//new product
                     $new_from_date = date("m/d/Y"); 
                     $new_to_date = date('m/d/Y',strtotime("+30 day"));
    
                     $product->setNewsFromDate($new_from_date); 
                     $product->setNewsToDate($new_to_date); 
                     $product->save();
                }
            }
    }
    ?>

In this case, you can use the event like:

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

<event name="catalog_product_save_after">
    <observer name="my_custom_product_save_observer" instance="Vendor\module\Observer\Productsaveafter" />
</event>  

And at this observer,you can get identify by below code

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

<?php
    
        namespace Vendor\Module\Observer;
    
        use Magento\Framework\Event\ObserverInterface;
    
        class Productsaveafter implements ObserverInterface
        {
           public function execute(\Magento\Framework\Event\Observer $observer)
            {
                 /** @var Product $product */
                $product = $observer->getEvent()->getProduct();
                if($product->isObjectNew() )
                {//new product
                     $new_from_date = date("m/d/Y"); 
                     $new_to_date = date('m/d/Y',strtotime("+30 day"));
    
                     $product->setNewsFromDate($new_from_date); 
                     $product->setNewsToDate($new_to_date); 
                }
            }
    }
    ?>

In this case, you can use the event like:

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

<event name="catalog_product_save_after">
    <observer name="my_custom_product_save_observer" instance="Vendor\module\Observer\Productsaveafter" />
</event>  

And at this observer,you can get identify by below code

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

<?php
    
        namespace Vendor\Module\Observer;
    
        use Magento\Framework\Event\ObserverInterface;
    
        class Productsaveafter implements ObserverInterface
        {
           public function execute(\Magento\Framework\Event\Observer $observer)
            {
                 /** @var Product $product */
                $product = $observer->getEvent()->getProduct();
                if($product->isObjectNew() )
                {//new product
                     $new_from_date = date("m/d/Y"); 
                     $new_to_date = date('m/d/Y',strtotime("+30 day"));
    
                     $product->setNewsFromDate($new_from_date); 
                     $product->setNewsToDate($new_to_date); 
                     $product->save();
                }
            }
    }
    ?>
updated answer
Source Link
Gohil Rajesh
  • 3.1k
  • 1
  • 14
  • 25

In this case, you can use the event like:

catalog_product_save_after

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

<event name="catalog_product_save_after">
    <observer name="my_custom_product_save_observer" instance="Vendor\module\Observer\Productsaveafter" />
</event>  

And at this observer,you can get identify by below code

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

<?php
    
        namespace Vendor\Module\Observer;
    
        use Magento\Framework\Event\ObserverInterface;
    
        class Productsaveafter implements ObserverInterface
        {
           public function execute(\Magento\Framework\Event\Observer $observer)
            {
                 /** @var Product $product */
                $product = $observer->getEvent()->getProduct();
                if($product->isObjectNew() )
                {//new product
                     $new_from_date = date("m/d/Y"); 
                     $new_to_date = date('m/d/Y',strtotime("+30 day"));
    
                     $product->setNewsFromDate($new_from_date); 
                     $product->setNewsToDate($new_to_date); 
                }
            }
    }
    ?>

In this case, you can use the event like:

catalog_product_save_after

And at this observer,you can get identify by below code

   public function execute(\Magento\Framework\Event\Observer $observer)
    {
         /** @var Product $product */
        $product = $observer->getEvent()->getProduct();
        if($product->isObjectNew() )
        {//new product
             $new_from_date = date("m/d/Y"); 
             $new_to_date = date('m/d/Y',strtotime("+30 day"));

             $product->setNewsFromDate($new_from_date); 
             $product->setNewsToDate($new_to_date); 
        }
    }

In this case, you can use the event like:

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

<event name="catalog_product_save_after">
    <observer name="my_custom_product_save_observer" instance="Vendor\module\Observer\Productsaveafter" />
</event>  

And at this observer,you can get identify by below code

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

<?php
    
        namespace Vendor\Module\Observer;
    
        use Magento\Framework\Event\ObserverInterface;
    
        class Productsaveafter implements ObserverInterface
        {
           public function execute(\Magento\Framework\Event\Observer $observer)
            {
                 /** @var Product $product */
                $product = $observer->getEvent()->getProduct();
                if($product->isObjectNew() )
                {//new product
                     $new_from_date = date("m/d/Y"); 
                     $new_to_date = date('m/d/Y',strtotime("+30 day"));
    
                     $product->setNewsFromDate($new_from_date); 
                     $product->setNewsToDate($new_to_date); 
                }
            }
    }
    ?>
Source Link
Gohil Rajesh
  • 3.1k
  • 1
  • 14
  • 25

In this case, you can use the event like:

catalog_product_save_after

And at this observer,you can get identify by below code

   public function execute(\Magento\Framework\Event\Observer $observer)
    {
         /** @var Product $product */
        $product = $observer->getEvent()->getProduct();
        if($product->isObjectNew() )
        {//new product
             $new_from_date = date("m/d/Y"); 
             $new_to_date = date('m/d/Y',strtotime("+30 day"));

             $product->setNewsFromDate($new_from_date); 
             $product->setNewsToDate($new_to_date); 
        }
    }