类别编辑呼叫后的事件回调

时间:2019-02-11 00:48:58

标签: magento2 magento-2.0 magento2.2 magento2.1

您好,在“类别项目”被编辑后如何呼叫观察者?基本上,我有一个观察者,可以列出每个事件中的所有类别,例如保存新类别,删除类别,移动类别位置和编辑类别。

但是问题是在编辑类别时,它会检索类别的最后内容。假设当前类别名称为 Test ,保存内容后,我将其更改为 Test 101 。生成的文件所保存的内容为 Test >应该是测试101 ,以下是我的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>

这是检索子类别的代码

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 
}

关于如何检索新编辑的类别的任何想法?我在想编辑事件后回调或其他什么东西

1 个答案:

答案 0 :(得分:0)

好,我知道了。对于那些正在寻找与我相同的解决方案的人,他们没有使用aggregationOperations.add( Aggregation.project("blabla", ...). and(DateOperators.DateToString.dateOf("timeCreated").toString("%Y-%m-%d")); ,而是使用category_prepare_ajax_response,这是在保存完成后调用的,因此您可以检索最新保存的内容。我的events.xml看起来像这样

catalog_category_save_after