Reputation: 283
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?
Upvotes: 1
Views: 216
Reputation: 13161
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);
}
}
Upvotes: 1