【问题标题】:Shopware 6 : How to use the `CategoryIndexerEvent` available in Shopware\Core\Content\Category\EventShopware 6 : 如何使用 Shopware\Core\Content\Category\Event 中的 `CategoryIndexerEvent`
【发布时间】:2021-01-11 09:05:54
【问题描述】:

我想在我的插件中使用Shopware\Core\Content\Category\Event\CategoryIndexerEvent.php

有人知道如何使用这个活动吗?

【问题讨论】:

    标签: shopware6


    【解决方案1】:

    您需要创建一个EventSubscriber。对于您提到的事件,这看起来像这样。

    首先你需要一个事件订阅者:

    <?php declare(strict_types=1);
    
    namespace MyPlugin;
    
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    use Shopware\Core\Content\Category\CategoryEvents;
    use Shopware\Core\Content\Category\Event\CategoryIndexerEvent;
    
    class MySubscriber implements EventSubscriberInterface
    {
        public static function getSubscribedEvents(): array
        {
            return [
                CategoryEvents::CATEGORY_INDEXER_EVENT => 'doMyStuff'
            ];
        }
    
        public function doMyStuff(CategoryIndexerEvent $event): void
        {
            $indexedCategories = $event->getIds();
            // Do stuff
        }
    }
    

    其次,您需要在 DIC 中注册此订阅者(plugin/src/Resources/config/services.xml):

    <!-- ... -->
    <service id="MyPlugin\MySubscriber">
        <tag name="kernel.event_subscriber"/>
    </service>
    <!-- ... -->
    

    【讨论】:

    • 嘿,谢谢你的回答。你知道这个事件什么时候执行吗?我的意思是添加/更新类别?
    • 分类索引后立即触发此事件。这是一个异步过程。如果您想对每个类别进行更改或写入,请选择 CategoryEvents::CATEGORY_WRITTEN_EVENT。
    猜你喜欢
    • 2019-02-08
    • 2021-01-12
    • 2021-11-22
    • 2021-08-09
    • 2021-01-05
    • 2022-12-17
    • 2022-10-23
    • 2022-09-23
    • 2021-11-04
    相关资源
    最近更新 更多