You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Shopware 6.6.2.0中注册product_review.written webhook遇阻求助

解决Shopware 6.6.2.0注册商品评论Webhook的方案

方案一:使用App Scripts监听事件触发Webhook

Shopware 6.6的App Scripts功能支持直接监听系统事件,即便该事件未被标记为可挂钩Webhook。操作步骤如下:

  1. 在你的App目录下创建Resources/scripts文件夹,新建脚本文件(例如product-review-written.twig)。
  2. 编写脚本内容:
{% 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 %}
  1. 在App的manifest.xml中声明该脚本:
<scripts>
    <script>Resources/scripts/product-review-written.twig</script>
</scripts>
  1. 重新安装App,用户提交新评论时脚本会自动触发并调用你的Webhook。

方案二:通过PHP插件中转事件

如果需要更复杂的逻辑处理,可以编写轻量PHP插件监听事件后转发到Webhook:

  1. 创建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()
            ]
        ]);
    }
}
  1. 在插件的services.xml中注册订阅者:
<service id="YourVendor\ProductReviewWebhook\ProductReviewSubscriber">
    <argument type="service" id="Symfony\Contracts\HttpClient\HttpClientInterface"/>
    <tag name="kernel.event_subscriber"/>
</service>
  1. 安装插件后,新评论提交时会自动触发Webhook调用。

注意事项

  • 确保Shopware服务器能访问你的Webhook地址,本地开发可使用ngrok等工具暴露端口。
  • Webhook处理逻辑需做好异常捕获,避免影响评论提交流程。
  • App Scripts的HTTP请求有默认超时限制,可在脚本中手动设置超时参数。

内容的提问来源于stack exchange,提问作者Saerdn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 03:22:19