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:
- Event not triggered when new product added in admin : Magento 2
- https://magecomp.com/blog/perform-operation-product-magento-2/
- Magento 2: how to insert data when submit product in backend?
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
Namespace\Custom\etc\module.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>
Namespace\Custom\registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Namespace_Custom',
__DIR__
);
Namespace\Custom\etc\adminhtml\events.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="Namespace\Custom\Observer\Adminhtml\ProductSaveAfter" />
</event>
</config>
Namespace\Custom\Observer\Adminhtml\ProductSaveAfter.php
<?php
namespace Namespace\Custom\Observer\Adminhtml;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class ProductSaveAfter implements ObserverInterface
{
/**
* @var ObjectManagerInterface
*/
protected $_objectManager;
/**
* @param \Magento\Framework\ObjectManagerInterface $objectManager
*/
public function __construct(
\Magento\Framework\ObjectManagerInterface $objectManager
) {
$this->_objectManager = $objectManager;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
$product = $observer->getEvent()->getProduct();
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
if($product->isObjectNew()){
$productId = $product->getId();
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$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.
catalog_product_edit_action
fired both on the vendor's side and in the admin panel, but no sign of my Observer (I did check this prior to changing tonew_action
). I'm not actually seeingcatalog_product_new_action
event being fired on either vendor or admin side.catalog_product_new_action
call when you go to "Create a new product" page,catalog_product_edit_action
call when you go to "Edit a product" page. I'll write the answer to help you resolve your issue.