custom/plugins/AcrisSuppliesUpsellingCS/src/Storefront/Subscriber/ProductLoadedSubscriber.php line 56

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\SuppliesUpselling\Storefront\Subscriber;
  3. use Acris\SuppliesUpselling\Components\CrossSellingGlobal\CrossSellingGlobalGateway;
  4. use Acris\SuppliesUpselling\Components\CrossSellingGlobal\CrossSellingGlobalService;
  5. use Shopware\Core\Content\Product\SalesChannel\CrossSelling\AbstractProductCrossSellingRoute;
  6. use Shopware\Core\Content\Product\SalesChannel\CrossSelling\CrossSellingElementCollection;
  7. use Shopware\Core\Framework\Struct\ArrayEntity;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. class ProductLoadedSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var CrossSellingGlobalService
  17.      */
  18.     private $crossSellingGlobalService;
  19.     /**
  20.      * @var CrossSellingGlobalGateway
  21.      */
  22.     private $crossSellingGlobalGateway;
  23.     /**
  24.      * @var SystemConfigService
  25.      */
  26.     private $systemConfigService;
  27.     /**
  28.      * @var AbstractProductCrossSellingRoute
  29.      */
  30.     private $crossSellingRoute;
  31.     public function __construct(
  32.         CrossSellingGlobalService $crossSellingGlobalService,
  33.         CrossSellingGlobalGateway $crossSellingGlobalGateway,
  34.         SystemConfigService $systemConfigService,
  35.         AbstractProductCrossSellingRoute $crossSellingRoute)
  36.     {
  37.         $this->crossSellingGlobalService $crossSellingGlobalService;
  38.         $this->crossSellingGlobalGateway $crossSellingGlobalGateway;
  39.         $this->systemConfigService $systemConfigService;
  40.         $this->crossSellingRoute $crossSellingRoute;
  41.     }
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.             ProductPageLoadedEvent::class => [
  46.                 ['productLoaded'400]
  47.             ]
  48.         ];
  49.     }
  50.     public function productLoaded(ProductPageLoadedEvent $event): void
  51.     {
  52.         $product $event->getPage()->getProduct();
  53.         if ($event->getPage()->getCmsPage() && $event->getPage()->getCmsPage()->getType() === "product_detail") {
  54.             $enableAsynchronousLoading $this->systemConfigService->get('AcrisSuppliesUpsellingCS.config.enableAsynchronousLoading'$event->getSalesChannelContext()->getSalesChannel()->getId());
  55.             if($enableAsynchronousLoading){
  56.                 $event->getPage()->setCrossSellings(new CrossSellingElementCollection());
  57.             }else{
  58.                 $crossSellings $this->crossSellingRoute->load($product->getId(), new Request(), $event->getSalesChannelContext(), new Criteria());
  59.                 $event->getPage()->setCrossSellings($crossSellings->getResult());
  60.             }
  61.         }
  62.         $enableAsynchronousLoading $this->systemConfigService->get('AcrisSuppliesUpsellingCS.config.enableAsynchronousLoading'$event->getSalesChannelContext()->getSalesChannel()->getId());
  63.         if($enableAsynchronousLoading){
  64.             return;
  65.         }
  66.         $configuratorResult $this->crossSellingGlobalGateway->getGlobalCrossSellings($event->getContext());
  67.         if($configuratorResult->count() === 0) {
  68.             return;
  69.         }
  70.         $crossSellings $this->crossSellingGlobalService->getCrossSellingsGlobalForProductId($configuratorResult$product->getId(), $event->getSalesChannelContext(), true);
  71.         $product->addExtension('acrisCrossSellingGlobal', new ArrayEntity([
  72.             'crossSellings' => $crossSellings
  73.         ]));
  74.     }
  75. }