Skip to content

Magento 2.4 – Observer For Catalog Product Save After Not Working

I have referenced a few different questions here and other articles online, but cannot for the life of me determine what is going wrong.

Referenced:

Context:
Website is a multi-vendor marketplace. I’m looking to save a timestamp at the moment a new product is added to the catalog by a vendor.

Desired result:
Upon a new product being created, the Observer will insert the datetime to a custom database table.

I understand this datetime is already stored in catalog_product_entity, but I need it saved in a separate table, as the datetime will be changed periodically for each product. The $vendorId portion can be ignored, which is why I’m simply using a static integer for that column until I’m able to get this working at all. Please also excuse the rudimentary method of saving said data to the table. Again, I know for a fact that method works elsewhere, which is why I’m using it here. To ensure that it’s not the issue, but something else wider in scope.

Actual Result: Data is not inserted into the custom database table.

Files
NamespaceCustometcmodule.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
      <module name="Namespace_Custom" setup_version="0.0.1">
        <sequence>
             <module name="Magento_Catalog"/>
         </sequence>
      </module>
    </config>

NamespaceCustomregistration.php

<?php

MagentoFrameworkComponentComponentRegistrar::register(
    MagentoFrameworkComponentComponentRegistrar::MODULE, 'Namespace_Custom',
    __DIR__
);

NamespaceCustometcadminhtmlevents.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_new_action">
        <observer name="Namespace_Custom_Product_Save_After" instance="NamespaceCustomObserverAdminhtmlProductSaveAfter" />
    </event>
</config>

NamespaceCustomObserverAdminhtmlProductSaveAfter.php

    <?php

    namespace NamespaceCustomObserverAdminhtml;

    use MagentoFrameworkEventObserver;
    use MagentoFrameworkEventObserverInterface;

    class ProductSaveAfter implements ObserverInterface
    {
      /**
       * @var ObjectManagerInterface
      */
       protected $_objectManager;

      /**
      * @param MagentoFrameworkObjectManagerInterface $objectManager
      */
    public function __construct(
        MagentoFrameworkObjectManagerInterface $objectManager
    ) {
        $this->_objectManager = $objectManager;
    }

    public function execute(MagentoFrameworkEventObserver $observer)
    {
     
        $product = $observer->getEvent()->getProduct();
        $objectManager = MagentoFrameworkAppObjectManager::getInstance();

        if($product->isObjectNew()){
          $productId = $product->getId();        


          $resource = $objectManager->get('MagentoFrameworkAppResourceConnection');
          $connection = $resource->getConnection();
          $table = $resource->getTableName('catalog_product_custom');

          $query = "INSERT INTO " . $table . " (entity_id, vendor_id, date_time)
          VALUES ($productId, 99, UNIX_TIMESTAMP())";
          $connection->query($query);
        }

    }
}

I have also tried using catalog_product_edit_action event.