I did it like this but it does not show on the product slider so how can I do that?
You may decorate the ProductSliderCmsElementResolver
to alter the criteria and results of the slider element.
<service id="MyPlugin\Core\Content\Product\Cms\ProductSliderCmsElementResolverDecorator" decorates="Shopware\Core\Content\Product\Cms\ProductSliderCmsElementResolver">
<argument type="service" id="MyPlugin\Core\Content\Product\Cms\ProductSliderCmsElementResolverDecorator.inner"/>
</service>
class ProductSliderCmsElementResolverDecorator implements CmsElementResolverInterface
{
private CmsElementResolverInterface $decorated;
public function __construct(CmsElementResolverInterface $decorated)
{
$this->decorated = $decorated;
}
public function getType(): string
{
return $this->decorated->getType();
}
public function collect(CmsSlotEntity $slot, ResolverContext $resolverContext): ?CriteriaCollection
{
$collection = $this->decorated->collect($slot, $resolverContext);
if (!$collection) {
return null;
}
foreach ($collection->all() as $criterias) {
foreach ($criterias as $criteria) {
$criteria->addAggregation(
new TermsAggregation('review_count', 'product.id', null, null, new CountAggregation('review_count', 'product.productReviews.id'))
);
}
}
return $collection->all() ? $collection : null;
}
public function enrich(CmsSlotEntity $slot, ResolverContext $resolverContext, ElementDataCollection $result): void
{
$this->decorated->enrich($slot, $resolverContext, $result);
// results from product streams
$entitySearchResult = $result->get('product-slider-entity-fallback_' . $slot->getUniqueIdentifier());
if ($entitySearchResult === null) {
// manual results
$entitySearchResult = $result->get('product-slider_' . $slot->getUniqueIdentifier());
}
if ($entitySearchResult === null) {
return;
}
foreach ($entitySearchResult->getAggregations() as $aggregation) {
if ($aggregation->getName() === 'review_count') {
foreach ($aggregation->getBuckets() as $bucket) {
/** @var SalesChannelProductEntity $product */
$product = $entitySearchResult->getEntities()->get($bucket->getKey());
if (!$product) {
continue;
}
$text = new TextStruct();
$text->setContent((string) $bucket->getResult()->getCount());
$product->addExtension('review_count', $text);
}
}
}
}
}