Shopware 6.6.2.0中注册product_review.written webhook遇阻求助
解决Shopware 6.6.2.0注册商品评论Webhook的方案
方案一:使用App Scripts监听事件触发Webhook
Shopware 6.6的App Scripts功能支持直接监听系统事件,即便该事件未被标记为可挂钩Webhook。操作步骤如下:
- 在你的App目录下创建
Resources/scripts文件夹,新建脚本文件(例如product-review-written.twig)。 - 编写脚本内容:
{% block script %} {% do services.event.subscribe('product_review.written', 'handleProductReviewWritten') %} {% macro handleProductReviewWritten(event) %} {% set review = event.productReview %} {# 发送请求到你的Webhook地址 #} {% set response = services.http.post('https://your-webhook-url.com', { 'reviewId': review.id, 'productId': review.productId, 'author': review.author, 'rating': review.rating, 'content': review.content }) %} {% endmacro %} {% endblock %}
- 在App的
manifest.xml中声明该脚本:
<scripts> <script>Resources/scripts/product-review-written.twig</script> </scripts>
- 重新安装App,用户提交新评论时脚本会自动触发并调用你的Webhook。
方案二:通过PHP插件中转事件
如果需要更复杂的逻辑处理,可以编写轻量PHP插件监听事件后转发到Webhook:
- 创建PHP插件,编写事件订阅者:
<?php namespace YourVendor\ProductReviewWebhook; use Shopware\Core\Content\Product\Event\ProductReviewWrittenEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; class ProductReviewSubscriber implements EventSubscriberInterface { private HttpClientInterface $httpClient; public function __construct(HttpClientInterface $httpClient) { $this->httpClient = $httpClient; } public static function getSubscribedEvents(): array { return [ ProductReviewWrittenEvent::class => 'onProductReviewWritten', ]; } public function onProductReviewWritten(ProductReviewWrittenEvent $event): void { $review = $event->getProductReview(); // 转发评论数据到你的Webhook地址 $this->httpClient->request('POST', 'https://your-app-webhook-url.com', [ 'json' => [ 'reviewId' => $review->getId(), 'productId' => $review->getProductId(), 'author' => $review->getAuthor(), 'rating' => $review->getRating(), 'content' => $review->getContent(), 'status' => $review->getStatus() ] ]); } }
- 在插件的
services.xml中注册订阅者:
<service id="YourVendor\ProductReviewWebhook\ProductReviewSubscriber"> <argument type="service" id="Symfony\Contracts\HttpClient\HttpClientInterface"/> <tag name="kernel.event_subscriber"/> </service>
- 安装插件后,新评论提交时会自动触发Webhook调用。
注意事项
- 确保Shopware服务器能访问你的Webhook地址,本地开发可使用ngrok等工具暴露端口。
- Webhook处理逻辑需做好异常捕获,避免影响评论提交流程。
- App Scripts的HTTP请求有默认超时限制,可在脚本中手动设置超时参数。
内容的提问来源于stack exchange,提问作者Saerdn
相关产品推荐
相关产品推荐

