10

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>

The observers class is then called when a product is saved but I can not find any info on how to access the product that was saved. 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]
);

This Passes the controller as an argument. However I am unsure how to to use this to access the product.

I would like to get the products sku

23

If you want to $productobj after saving product from backend then you can easily use catalog_product_save_after event.

I am assuming that you already know how to create a module in M2.

Put this events.xml in below path

app\code\YOUR_NAMESPACE\YOURMODULE\etc\adminhtml

<?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_save_after">
        <observer name="test_name" instance="YOUR_NAMESPACE\YOUR_MODULENAME\Observer\Productsaveafter" />
    </event>
</config>

And put your Productsaveafter.php in below path

app\code\YOUR_NAMESPACE\YOURMODULE\Observer\

<?php

namespace YOURNAMESPACE\YOURMODULENAME\Observer;

use Magento\Framework\Event\ObserverInterface;

class Productsaveafter implements ObserverInterface
{    
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $_product = $observer->getProduct();  // you will get product object
        $_sku=$_product->getSku(); // for sku

    }   
}
  • Perfect, that's got it. Can I ask how I would find out about that event? I can't see it listed in the source code so didn't know it existed. – GuyC Dec 16 '15 at 13:57
  • You can find reference of events from this links cyrillschumacher.com/magento2-list-of-all-dispatched-events and magento.stackexchange.com/questions/87738/magento-2-events-list @GuyC – Keyur Shah Dec 16 '15 at 14:00
  • I did have that list as a reference but couldn't see it there either, hence I ended up using: controller_action_catalog_product_save_entity_after, is it omitted for any reason? – GuyC Dec 16 '15 at 14:02
  • 1
    Please note that if you put your event.xml file in adminhtml area then your observer will be not executed in other areas like REST or SOAP that represent web api – KAndy Dec 16 '15 at 17:08
  • 2
    @GuyC Finally found where it comes from. There is a specific event which uses an entity-specific “prefix”. In Magento, most of the core models redefine the internal variable “_eventPrefix”, so one can observe CRUD operations for specific models. To determine the prefix used for a model, you need to check the model source code. Check this: magento.stackexchange.com/questions/184772/… – Mehdi Jul 24 '17 at 15:41
0

This is quite old and I m not sure if anyone cares now but I was looking for this so:

I m not sure since when but definitely on M2.2.x that event is like this now:

$this->_eventManager->dispatch(
                    'controller_action_catalog_product_save_entity_after',
                    ['controller' => $this, 'product' => $product]
                );

which means it is giving you now the product too.

For your case you should have done this and you could get the product attributes as an array:

$post = $observer->getController();
        $data = $post->getRequest()->getPost();
        $productAsArray = $data['product'];
  • $product = $observer->getData('product'); should get you the Product – shobekhan Oct 7 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.