gpt4 book ai didi

shopware - 在 Shopware 产品列表页面上显示评论数

转载 作者:行者123 更新时间:2023-12-02 01:30:54 27 4
gpt4 key购买 nike

我想在产品列表页面(如产品详情页面)上显示产品评论的总数,如何在列表页面上获取该计数?

最佳答案

这不是那么微不足道。您需要编写一个插件来实现这一点。

在你的插件中你需要create a subscriber并收听与产品搜索和列表相关的事件。然后,您需要通过添加聚合来更改搜索条件,以及为列出的产品对象设置聚合值的搜索结果。

class CustomListingEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
ProductListingCriteriaEvent::class => [
['handleListingCriteria', -101],
],
ProductSearchCriteriaEvent::class => [
['handleListingCriteria', -101],
],
ProductListingResultEvent::class => [
['handleListingResult', 0],
],
ProductSearchResultEvent::class => [
['handleListingResult', 0],
],
];
}

public function handleListingCriteria(ProductListingCriteriaEvent $event): void
{
$criteria = $event->getCriteria();

$criteria->addAggregation(
new TermsAggregation('review_count', 'product.id', null, null, new CountAggregation('review_count', 'product.productReviews.id'))
);
}

public function handleListingResult(ProductListingResultEvent $event): void
{
/** @var TermsResult $aggregation */
foreach ($event->getResult()->getAggregations() as $aggregation) {
if ($aggregation->getName() === 'review_count') {
foreach ($aggregation->getBuckets() as $bucket) {
/** @var SalesChannelProductEntity $product */
$product = $event->getResult()->getEntities()->get($bucket->getKey());
if (!$product) {
continue;
}
// Ideally you should implement you own `Struct` extension
$text = new TextStruct();
$text->setContent((string) $bucket->getResult()->getCount());
$product->addExtension('review_count', $text);
}
}
}
}
}

对于此示例,它将计算与产品相关的所有评论,而不仅仅是那些活跃的评论。您可能还想查看有关 how to filter aggregations 的文档.

之后在产品框的模板中,您应该能够输出每个产品的评论数:

{{ product.extensions.review_count.content }}

关于shopware - 在 Shopware 产品列表页面上显示评论数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73333013/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com