custom/plugins/DvsnBundle/src/Subscriber/Storefront/Page/Product/ProductPageSubscriber.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * digitvision
  4.  *
  5.  * @category  digitvision
  6.  * @package   Shopware\Plugins\DvsnBundle
  7.  * @copyright (c) 2021 digitvision
  8.  */
  9. namespace Dvsn\Bundle\Subscriber\Storefront\Page\Product;
  10. use Dvsn\Bundle\Service\Bundle\BundleCollectorServiceInterface;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class ProductPageSubscriber implements EventSubscriberInterface
  15. {
  16.     private BundleCollectorServiceInterface $bundleCollectorService;
  17.     private SystemConfigService $systemConfigService;
  18.     public function __construct(
  19.         BundleCollectorServiceInterface $bundleCollectorService,
  20.         SystemConfigService $systemConfigService
  21.     ) {
  22.         $this->bundleCollectorService $bundleCollectorService;
  23.         $this->systemConfigService $systemConfigService;
  24.     }
  25.     /**
  26.      * {@inheritDoc}
  27.      */
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             ProductPageLoadedEvent::class => 'onPageLoaded'
  32.         ];
  33.     }
  34.     /**
  35.      * ...
  36.      *
  37.      * @param ProductPageLoadedEvent $event
  38.      */
  39.     public function onPageLoaded(ProductPageLoadedEvent $event)
  40.     {
  41.         // plugin has to be active
  42.         if ((boolean) $this->systemConfigService->get('DvsnBundle.config.status'$event->getSalesChannelContext()->getSalesChannel()->getId()) === false) {
  43.             // nothing to do
  44.             return;
  45.         }
  46.         // get params
  47.         $salesChannelContext $event->getSalesChannelContext();
  48.         $product $event->getPage()->getProduct();
  49.         $request $event->getRequest();
  50.         // get the product options
  51.         $bundles $this->bundleCollectorService->get(
  52.             $product,
  53.             $salesChannelContext
  54.         );
  55.         // assign to page
  56.         $event->getPage()->assign([
  57.             'dvsnBundle' => $bundles,
  58.             'dvsnBundleConfiguration' => $this->systemConfigService->get('DvsnBundle.config'$salesChannelContext->getSalesChannel()->getId())
  59.         ]);
  60.     }
  61. }