magento2magento-2.0magento2.2magento2.1

Events callback after category edit call


Hi how can I call an observer after the Category Item has been edited? Basically I have an observer that list all the categories in every event like saving new category, deleting category, moving category position and editing category.

But the problem is when editing a category its retrieving the last content of the category. Let say the current category name is Test and I change it to Test 101 upon saving the content the generated file is being saved having a content of Test which should be Test 101 Below is my 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="category_prepare_ajax_response">
        <observer name="category-edit" instance="Module\FrontName\Observer\CategoryEditObserver" />
    </event>  
</config>

And this is the code that retrieve the subcategories

public function getStoreCategories($storeManager)
{
    // $categories = $category_helper->getStoreCategories();
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $categoryFactory = $objectManager->create('Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
    $categories = $categoryFactory->create()                              
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('level', array('eq' => 2))
        ->addIsActiveFilter()
        ->setStore($storeManager->getStore()); //categories from current store will be 
}

Any idea on how to retrieve the newly edited categories? I was thinking of after edit events callback or something


Solution

  • Ok I got the answer. For those who is looking for the same solution what I did was instead of using category_prepare_ajax_response I used catalog_category_save_after this will be called after the saving is done thus you will be able to retrieve the latest saved content. My events.xml looks like this

    <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="category-edit" instance="TCM\HeaderMenu\Observer\CategoryEditObserver" />
        </event>
    </config>