如何访问shopware 6中实体相关事件订阅者中的Shopware\Core\System\SalesChannel\SalesChannelContext?

san*_*dip 1 shopware

我在 Shopware 6 的插件中构建了一个自定义订阅者,该订阅者可以订阅

\Shopware\Core\Content\Product\ProductEvents::PRODUCT_WRITTEN_EVENT = 'product.written';

public function onProductWrittenEntity(EntityWrittenEvent $event): void
{
        //$event->getContext() is returning the Shopware\Core\Framework\Context
}
Run Code Online (Sandbox Code Playgroud)

我想获取当前salesChannel具有当前写入的那些productId 的域URL。我怎样才能做到这一点?

小智 5

salesChannelId您可以通过运行以下代码来获取:

$event->getContext()->getSource()->getSalesChannelId()
Run Code Online (Sandbox Code Playgroud)

这样并将viasalesChannelId插入到您的订阅者中,您就可以从该销售渠道加载所需的信息。SalesChannelRepositoryservices.xml

  • 你能添加一个解释吗?这对OP有何帮助? (4认同)
  • OP 可能知道这一点,但其他第一次看到这个答案、有同样问题的人可能不知道。这就是为什么解释你的方法很重要。 (3认同)