Shopware 6.4自定义产品增删改事件日志无记录问题求助
解决Shopware 6.4产品操作事件日志记录问题
你的问题核心在于:自定义事件未被Shopware的事件日志系统识别,且未显式调用日志写入逻辑。Shopware的事件日志需通过EventLoggerInterface服务主动写入,而非仅分发自定义事件。以下是完整解决方案:
1. 核心修正思路
- 直接在
ProductWrittenEvent订阅者中处理日志写入,无需自定义事件 - 区分产品创建/更新/删除三种操作类型
- 获取真实操作人信息(替代硬编码的
test) - 调用Shopware内置的
EventLoggerInterface写入日志
2. 完整代码实现
订阅者类
<?php namespace YourPlugin\Subscriber; use Shopware\Core\Framework\Context; use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository; use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent; use Shopware\Core\Framework\Event\EventLoggerInterface; use Shopware\Core\System\User\UserEntity; use Shopware\Core\Content\Product\ProductEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class ProductEventLogSubscriber implements EventSubscriberInterface { public function __construct( private readonly EventLoggerInterface $eventLogger, private readonly EntityRepository $userRepository, private readonly EntityRepository $productRepository ) {} public static function getSubscribedEvents(): array { return [ ProductEvents::PRODUCT_WRITTEN_EVENT => 'onProductWritten', ]; } public function onProductWritten(EntityWrittenEvent $event): void { $context = $event->getContext(); foreach ($event->getWriteResults() as $writeResult) { // 区分操作类型 if ($writeResult->isCreated()) { $action = '创建'; } elseif ($writeResult->isUpdated()) { $action = '更新'; } elseif ($writeResult->isDeleted()) { $action = '删除'; } else { continue; } // 获取产品名称 $productId = $writeResult->getPrimaryKey(); $product = $this->productRepository->search( (new \Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria())->addFilter( new \Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter('id', $productId) ), $context )->first(); $productName = $product ? $product->getName() : '未知产品'; // 获取操作人信息 $user = $this->getCurrentUser($context); $operator = $user ? $user->getUsername() : '系统用户'; // 写入事件日志 $this->eventLogger->log( 'product.' . strtolower($action), [ 'product_id' => $productId, 'product_name' => $productName, 'operator' => $operator, 'action_time' => (new \DateTime())->format('Y-m-d H:i:s') ], $context ); } } private function getCurrentUser(Context $context): ?UserEntity { $userId = $context->getUserId(); if (!$userId) { return null; } return $this->userRepository->search( (new \Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria())->addFilter( new \Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter('id', $userId) ), $context )->first(); } }
服务配置(config/services.xml)
确保订阅者被正确注册:
<service id="YourPlugin\Subscriber\ProductEventLogSubscriber"> <argument type="service" id="Shopware\Core\Framework\Event\EventLoggerInterface"/> <argument type="service" id="user.repository"/> <argument type="service" id="product.repository"/> <tag name="kernel.event_subscriber"/> </service>
3. 关键说明
- 操作类型区分:通过
WriteResult的isCreated()/isUpdated()/isDeleted()方法判断产品操作类型 - 操作人获取:从
Context中提取用户ID,再查询用户实体获取用户名 - 日志写入:使用
EventLoggerInterface::log()方法,第一个参数为日志事件名称,第二个为自定义日志数据,第三个为上下文 - 产品信息:通过产品仓库查询产品名称,避免硬编码测试数据
4. 验证效果
执行产品的创建/更新/删除操作后,可在Shopware后台设置 > 系统 > 事件日志中查看对应记录,每条日志包含操作人、产品名称、操作类型和时间。
内容的提问来源于stack exchange,提问作者ciekals11
相关产品推荐
相关产品推荐

