Because in Magento 2.1.3 the category_after_save event is missing I created a afterExecute plugin for the category save plugin. If I save a existing category the request contains the entity_id, but if I create a new one the entity_id does not exist in the post parameters. Is there an other way to get this id?

  • You can easily get category id using url: First create category and open that category and see at the url or address bar of the browser. Here at the last of url you can see .../id/4 – PG Sutariya Feb 25 '17 at 4:12
  • I need to get the category id in my plugin not in the browser. – tho Feb 25 '17 at 9:03

Aside from extracting directly from the response object, there doesn't appear to be a publicly accessible point in the category save controller to get the entity_id for new instances.

The good news is, you can still utilize the after_save method in Magento 2.1.3. Since it extends Magento\Framework\Model\AbstractModel, events are generated using

public function afterSave()
    $this->_eventManager->dispatch($this->_eventPrefix . '_save_after', $this->_getEventData());

To utilize it, you'll just need to create an events file <module_directory>/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_category_save_after">
        <observer name="example_module_catalog_category_save_after" instance="Example\Module\Observer\Catalog\CategorySaveAfter"/>

And a corresponding Observer <module_directory>/Observer/Catalog/CategorySaveObserver.php

namespace Example\Module\Observer\Catalog;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class CategorySaveAfter implements ObserverInterface
    public function execute(Observer $observer)
        $category = $observer->getEvent()->getCategory();

This will give you access to the category data, regardless if it's new or existing.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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