Following a product save event I am trying to access the product within Magento 2.
I have configured the listener on:
controller_action_catalog_product_save_entity_after
the xml looks as follows:
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="controller_action_catalog_product_save_entity_after">
<observer name="wire_products_update" instance="TBS\WireProductsHook\Observer\WireProductsUpdate" />
</event>
</config>
When a product saved observers class is called. But how to access the saved product information. The observer class is as follows:
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace TBS\WireProductsHook\Observer;
use Magento\Framework\Event\ObserverInterface;
class WireProductsUpdate implements ObserverInterface {
public function execute( \Magento\Framework\Event\Observer $observer ) {
// echos name of event: controller_action_catalog_product_save_entity_after
echo 'name of event: ' . $observer->getEvent()->getName();
// tried using the same method as the save class: <magento>/module-catalog/Controller/Adminhtml/Product/Save.php on line 76
$controller = $observer->getData('controller');
// Fails: Fatal error: Cannot access protected property ...
$product = $controller->initializationHelper->initialize($controller->productBuilder->build($controller->getRequest()));
}
}
Within /module-catalog/Controller/Adminhtml/Product/Save.php the event is fired on line 114:
$this->_eventManager->dispatch(
'controller_action_catalog_product_save_entity_after',
['controller' => $this]
);
Controller pass as an argument. However, I am unsure how to to use this to access the product.
I would like to get the products SKU