<?php declare(strict_types=1);
namespace ThemeOkeOnline\Storefront\Page\Product;
use Shopware\Core\Content\Product\Exception\ReviewNotActiveExeption;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Product\ProductLoaderCriteriaEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Storefront\Page\Product\Review\ProductReviewLoader;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use ThemeOkeOnline\Core\Content\Product\ProductSiblingService;
class ProductPageLoadedEventSubscriber implements EventSubscriberInterface
{
/**
* @var ProductPageLoadedEvent
*/
private $event;
private $context;
private $page;
private $request;
/**
* @var ProductSiblingService
*/
private $productSiblingService;
/**
* @var ProductReviewLoader
*/
private $productReviewLoader;
/**
* @var SystemConfigService
*/
private $systemConfigService;
public function __construct(
ProductSiblingService $productSiblingService,
ProductReviewLoader $productReviewLoader,
SystemConfigService $systemConfigService
)
{
$this->productSiblingService = $productSiblingService;
$this->productReviewLoader = $productReviewLoader;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array
{
return [
'Shopware\Storefront\Page\Product\ProductPageLoadedEvent' => ['productLoaded']
];
}
public function productLoaded(ProductPageLoadedEvent $event): void
{
$this->addReviewDataToProductPage($event);
$this->addSiblingDataToProductPage($event);
}
public function addSiblingDataToProductPage(ProductPageLoadedEvent $event): void
{
$this->event = $event;
$context = $event->getContext();
$page = $event->getPage();
$product = $page->getProduct();
$configuratorSettings = $page->getConfiguratorSettings();
$data = $this->productSiblingService->getSiblingsForProduct($product, $context);
$page->addExtension('siblings', $data);
}
/*
* Somehow the reviews are not available when using our plugin, but we will get them back!
*/
public function addReviewDataToProductPage($event): void
{
$this->event = $event;
$this->context = $event->getSalesChannelContext();
$this->page = $event->getPage();
$this->request = $event->getRequest();
$this->checkReviewsActive();
$reviews = $this->getProductReviews();
$this->page->addExtension('reviews', $reviews);
}
private function getProductReviews(): Object
{
$product = $this->page->getProduct();
$reviews = $this->productReviewLoader->load($this->request, $this->context);
$reviews->setParentId($product->getParentId() ?? $product->getId());
return $reviews;
}
/**
* @throws ReviewNotActiveExeption
*/
private function checkReviewsActive(): void
{
$showReview = $this->systemConfigService->get('core.listing.showReview', $this->context->getSalesChannel()->getId());
if (!$showReview) {
throw new ReviewNotActiveExeption();
}
}
}