3

I have created one observer CategorySaveAfter.php file in my custom module.

And calling this observer on catalog_category_save_after event define in events.xml

But somehow its not calling when i save category using restApi

Its working fine when i save category from magento2 backend it calling observer staightforward.

But for Rest APi its not working .

Below code i have tried :

Vendor/Module/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="catalog_megamenu" instance="Vendor\ModuleName\Observer\CategorySaveAfter" />
    </event>
</config>

Vendor/Module/Observer/Categorysaveafter.php

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Vendor\ModuleName\Observer;

use Magento\Catalog\Model\Category;
use Magento\Framework\Event\ObserverInterface;

class Categorysaveafter implements ObserverInterface
{
    protected $test;

    public function __construct(\Vendor\Modulename\Model\ModelFactory $testMenu)
    {
        $this->test = $testMenu;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        echo "goes here";exit;       
    }
}

Yes i have added registration.php and module.xml file as well.

Update :

Forgot to mention that i have tried global level events.xml as well . Like etc/events.xml and etc/webapi_rest/events.xml . Still both way its not calling

Your help will be appreciated.

5
  • have you resolve this issue?
    – LucScu
    Commented Jul 18, 2017 at 6:57
  • @LucaS yes i have override their existing model and its method and then save this Commented Jul 18, 2017 at 9:23
  • great, so could you close the question writing your answer?
    – LucScu
    Commented Jul 19, 2017 at 7:21
  • 1
    @LucaS sure will do asap , Thanks for reminding me Commented Jul 19, 2017 at 7:42
  • Your class name is wrong it should be CategorySaveAfter not Categorysaveafter. as is used in the events.xml file.. Commented Jul 5, 2018 at 10:59

5 Answers 5

1

You have to override core CategoryRepository.php file to changes your custom requirement.

app/code/Package/Modulename/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\CategoryRepository">
        <plugin name="CategoryRepositoryRestPim" type="Package\Modulename\Model\CategoryRepository" />
   </type>
</config>

Inside app/code/Package/Modulename/Model/CategoryRepository.php,

<?php    
namespace Package\Modulename\Model;

class CategoryRepository 
{

 public function afterSave(\Magento\Catalog\Model\CategoryRepository $subject,$result)
    {
        var_dump($result->getData());
        $categoryId = $result->getId(); //get category id 
    }
}

Remove var/generation folder and check again.

5
  • Yes same way i have resolved this issue , Thanks for sharing the answer . Well explained :) Commented Jul 20, 2017 at 5:48
  • This answer not resolve why catalog_category_save_after event not trig from webapi_rest area...so is it a magento bug?
    – LucScu
    Commented Jul 20, 2017 at 17:57
  • @LucaS yes might be it is bug , because we have tried to call that event from each area. likewise we have puted event.xml in globally , locally and webapi_rest folder as well still its not worked and then we have done another way Commented Jul 21, 2017 at 12:00
  • great, thx for your update :)
    – LucScu
    Commented Jul 21, 2017 at 14:52
  • 2
    @Rakesh Jesadiya, code not working while saving category from backend magento 2.3.4 Commented Mar 19, 2020 at 7:52
0

You should change your event area for global area instead of adminhtml section.

Create events.xml at Vendor/Module/etc/ and then declare that event at that file .Also remove events.xml from Vendor/Module/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_category_save_after">
        <observer name="catalog_megamenu" instance="Vendor\ModuleName\Observer\CategorySaveAfter" />
    </event>
</config>
1
  • Forgot to mention that i have tried global level events.xml as well . Like etc/events.xml and etc/web_rest/events.xml . Still both way its not calling. Modify the question as well. Commented Jul 10, 2017 at 8:15
0

I think it not works from rest api call because you setted events.xml only for adminhtml area (see magento areas).

Try to define events.xml also in webapi_rest area or simply in <module-root>/etc directory as explained in docs.

7
  • Forgot to mention that i have tried global level events.xml as well . Like etc/events.xml and etc/web_rest/events.xml . Still both way its not calling. Modify the question as well. Commented Jul 10, 2017 at 8:15
  • Docs says that the folder must be /webapi_rest
    – LucScu
    Commented Jul 10, 2017 at 8:17
  • Did it mean outside of the etc folder ? also provide me doc link for - webapi_rest if possible ? Commented Jul 10, 2017 at 8:19
  • No sorry, you wrote etc/web_rest/events.xml folder must be etc/webapi_rest/events.xml
    – LucScu
    Commented Jul 10, 2017 at 8:20
  • sorry it was my typo mistake , yes it is in webapi_rest . But its still not calling Commented Jul 10, 2017 at 8:22
0

I tried the accepted answer with Magento 2.3.4 and nothing was happening on that plugin so i searched the core Magento_Catalog Module and i reached the way to make the plugin work for me this is based on @Rakesh Answer that helped me start, First

Vendor/Module/etc/di.xml

here is the code

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Category">
        <plugin name="categoryRepositorySaveAfter" type="Vendor\Module\Plugin\SaveCategory" />
    </type>

</config>

then. the plugin class

<?php    
namespace Vendor\Module\Plugin;

class SaveCategory 
{

 public function afterSave(\Magento\Catalog\Model\Category $subject, $category)
    {
        $categoryId = $category->getId();
    }
}
1
  • @hitesh-balpande , i think this may help you with magento 2.3.4
    – mahmudy-91
    Commented May 28, 2020 at 7:02
0

In case a plugin doens't work and you need to use an Observer:

The code in question with using observer will work if you change file name and class name from Categorysaveafter to CategorySaveAfter to match the capitalisation of the observer defined in events.xml.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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