<?php declare(strict_types=1);
namespace Acris\SuppliesUpselling\Storefront\Subscriber;
use Acris\SuppliesUpselling\Components\CrossSellingGlobal\CrossSellingGlobalGateway;
use Acris\SuppliesUpselling\Components\CrossSellingGlobal\CrossSellingGlobalService;
use Shopware\Core\Content\Product\SalesChannel\CrossSelling\AbstractProductCrossSellingRoute;
use Shopware\Core\Content\Product\SalesChannel\CrossSelling\CrossSellingElementCollection;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
class ProductLoadedSubscriber implements EventSubscriberInterface
{
/**
* @var CrossSellingGlobalService
*/
private $crossSellingGlobalService;
/**
* @var CrossSellingGlobalGateway
*/
private $crossSellingGlobalGateway;
/**
* @var SystemConfigService
*/
private $systemConfigService;
/**
* @var AbstractProductCrossSellingRoute
*/
private $crossSellingRoute;
public function __construct(
CrossSellingGlobalService $crossSellingGlobalService,
CrossSellingGlobalGateway $crossSellingGlobalGateway,
SystemConfigService $systemConfigService,
AbstractProductCrossSellingRoute $crossSellingRoute)
{
$this->crossSellingGlobalService = $crossSellingGlobalService;
$this->crossSellingGlobalGateway = $crossSellingGlobalGateway;
$this->systemConfigService = $systemConfigService;
$this->crossSellingRoute = $crossSellingRoute;
}
public static function getSubscribedEvents()
{
return [
ProductPageLoadedEvent::class => [
['productLoaded', 400]
]
];
}
public function productLoaded(ProductPageLoadedEvent $event): void
{
$product = $event->getPage()->getProduct();
if ($event->getPage()->getCmsPage() && $event->getPage()->getCmsPage()->getType() === "product_detail") {
$enableAsynchronousLoading = $this->systemConfigService->get('AcrisSuppliesUpsellingCS.config.enableAsynchronousLoading', $event->getSalesChannelContext()->getSalesChannel()->getId());
if($enableAsynchronousLoading){
$event->getPage()->setCrossSellings(new CrossSellingElementCollection());
}else{
$crossSellings = $this->crossSellingRoute->load($product->getId(), new Request(), $event->getSalesChannelContext(), new Criteria());
$event->getPage()->setCrossSellings($crossSellings->getResult());
}
}
$enableAsynchronousLoading = $this->systemConfigService->get('AcrisSuppliesUpsellingCS.config.enableAsynchronousLoading', $event->getSalesChannelContext()->getSalesChannel()->getId());
if($enableAsynchronousLoading){
return;
}
$configuratorResult = $this->crossSellingGlobalGateway->getGlobalCrossSellings($event->getContext());
if($configuratorResult->count() === 0) {
return;
}
$crossSellings = $this->crossSellingGlobalService->getCrossSellingsGlobalForProductId($configuratorResult, $product->getId(), $event->getSalesChannelContext(), true);
$product->addExtension('acrisCrossSellingGlobal', new ArrayEntity([
'crossSellings' => $crossSellings
]));
}
}