Magento2: Intercept category moved inside tree

by gianis6   Last Updated August 24, 2018 17:09 PM

I am trying to intercept when a category is moved inside the category tree. At the beginning I thought to observe the event catalog_category_tree_move_after with an Observer class but I found that this event has been removed from Magento 2. In the CHANGELOG file it is said to use plugins instead of observers.

I tried to do with plugin with no success: this is the di.xml file

<?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="MoveCategoryTree" type="Vendor\Module\Model\Plugin\Category" sortOrder="1" />
    </type>
</config>

this is the Vendor/Module/Model/plugin/Category.php class

namespace Vendor\Module\Model\Plugin;

class Category
{
    public function afterCatalogCategoryTreeMove(\Magento\Catalog\Model\Category $category, $result) {
        echo 'Testing...';
        exit();
    }
}

I tried also to rename the function afterCatalogCategoryTreeMove in afterTreeMove but nothing works also in this way.

is this the correct way to intercept this event?



Answers 1


I looked at this briefly and couldn't find the method you were trying to write a plugin for. The method move does exist and should be appropriate for what you're trying to do, try the code below instead:

namespace Vendor\Module\Model\Plugin;

class Category
{
    public function afterMove(\Magento\Catalog\Model\Category $subject, $result) {
        echo 'Testing...';
        exit();
    }
}

I hope that helps!

jamil
jamil
August 24, 2018 21:12 PM

Related Questions


Magento 2.1: Get ID of saved category

Updated February 24, 2017 19:09 PM


How to create plugin for observer Magento 2?

Updated October 30, 2018 13:09 PM

Magento 2: Plugin vs Observer

Updated May 15, 2018 22:09 PM

How to create plugin for observer magento2

Updated June 21, 2018 17:09 PM