‘page.reviews.totalReviews’ 变量在 Shopware 6.5.x 中返回 null,但在 6.4.x 中有效。

huangapple go评论65阅读模式
英文:

Why does the 'page.reviews.totalReviews' variable return null in Shopware 6.5.x but works in 6.4.x?

问题

在我们的Shopware主题(版本6.4.x)中,我们一直在使用变量page.reviews.totalReviews来获取产品的总评论数量。然而,在升级到Shopware 6.5.x后,这个变量返回null而不是预期的值。(我们在自定义CMS页面中使用了产品类型)

以下是我们在代码中如何使用该变量的示例:

{% sw_include '@Storefront/storefront/page/product-detail/buy-widget.html.twig' with {'totalReviews': page.reviews.totalReviews} %}

是否有人能够解释为什么这个变量在Shopware 6.5.x中不再按预期工作,并提出可能的解决方案或在新版本中检索总评论数量的替代方法?

英文:

In our Shopware theme (version 6.4.x), we have been using the variable page.reviews.totalReviews to retrieve the total number of reviews for a product. However, after upgrading to Shopware 6.5.x, this variable is returning null instead of the expected value. (We use a custom CMS page with type product)

Here is an example of how we are using the variable in our code:

{% sw_include '@Storefront/storefront/page/product-detail/buy-widget.html.twig' with {'totalReviews': page.reviews.totalReviews} %}

Could someone explain why this variable is no longer working as expected in Shopware 6.5.x and suggest any possible solutions or alternative approaches to retrieve the total number of reviews in the newer version?

答案1

得分: 1

这是要翻译的内容:

"This was changed a while ago, to no longer load the reviews if the product page has been assigned a CMS layout. Since the layout may not necessarily need the reviews, the CMS element resolvers should fetch data when needed. If you used the default blocks for the product detail page, the resolver of the element for the buy box will then be responsible for loading the reviews count, as it is the one element making use of it.

You can either create such a resolver for your custom CMS page/element or you can add a subscriber and listen to the ProductPageLoadedEvent. You can then inject ProductReviewLoader and use it to fetch the reviews and set them to the page object."

请注意,代码部分不在翻译范围内。

英文:

This was changed a while ago, to no longer load the reviews if the product page has been assigned a CMS layout. Since the layout may not necessarily need the reviews, the CMS element resolvers should fetch data when needed. If you used the default blocks for the product detail page, the resolver of the element for the buy box will then be responsible for loading the reviews count, as it is the one element making use of it.

You can either create such a resolver for your custom CMS page/element or you can add a subscriber and listen to the ProductPageLoadedEvent. You can then inject ProductReviewLoader and use it to fetch the reviews and set them to the page object.

<service id="SwagBasicExample\Subscriber\MySubscriber">
    <argument type="service" id="Shopware\Storefront\Page\Product\Review\ProductReviewLoader"/>
    <tag name="kernel.event_subscriber"/>
</service>
<?php declare(strict_types=1);

namespace SwagBasicExample\Subscriber;

use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Storefront\Page\Product\Review\ProductReviewLoader;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class MySubscriber implements EventSubscriberInterface
{
    public function __construct(private readonly ProductReviewLoader $productReviewLoader) 
    {
    }
    
    public static function getSubscribedEvents(): array
    {
        return [
            ProductPageLoadedEvent::class => 'onProductPageLoaded',
        ];
    }

    public function onProductPageLoaded(ProductPageLoadedEvent $event): void
    {
        $product = $event->getPage()->getProduct();
        $event->getRequest()->request->set('parentId', $product->getParentId());
        $reviews = $this->productReviewLoader->load($event->getRequest(), $event->getSalesChannelContext());
        $reviews->setParentId($product->getParentId() ?? $product->getId());

        $event->getPage()->setReviews($reviews);
    }
}

huangapple
  • 本文由 发表于 2023年7月4日 19:56:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76612387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定