custom/plugins/DvsnProductOption/src/Subscriber/Storefront/Page/Product/ProductPageSubscriber.php line 98

Open in your IDE?
  1. <?php
  2. /**
  3.  * digitvision
  4.  *
  5.  * @category  digitvision
  6.  * @package   Shopware\Plugins\DvsnProductOption
  7.  * @copyright (c) 2020 digitvision
  8.  */
  9. namespace Dvsn\ProductOption\Subscriber\Storefront\Page\Product;
  10. use Dvsn\ProductOption\Core\Content\ProductOption\ProductOptionEntity;
  11. use Dvsn\ProductOption\Service\ProductOptionCollectorServiceInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  13. use Shopware\Core\System\SystemConfig\SystemConfigService;
  14. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\Session\Session;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. class ProductPageSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * ...
  22.      *
  23.      * @var ProductOptionCollectorServiceInterface
  24.      */
  25.     protected $productOptionCollectorService;
  26.     /**
  27.      * ...
  28.      *
  29.      * @var EntityRepository
  30.      */
  31.     protected $productOptionRepository;
  32.     /**
  33.      * ...
  34.      *
  35.      * @var SystemConfigService
  36.      */
  37.     protected $systemConfigService;
  38.     /**
  39.      * ...
  40.      *
  41.      * @var Session
  42.      */
  43.     protected $session;
  44.     /**
  45.      * ...
  46.      *
  47.      * @var TranslatorInterface
  48.      */
  49.     protected $translator;
  50.     /**
  51.      * ...
  52.      *
  53.      * @param ProductOptionCollectorServiceInterface $productOptionCollectorService
  54.      * @param EntityRepository $productOptionRepository
  55.      * @param SystemConfigService $systemConfigService
  56.      * @param Session $session
  57.      * @param TranslatorInterface $translator
  58.      */
  59.     public function __construct(
  60.         ProductOptionCollectorServiceInterface $productOptionCollectorService,
  61.         EntityRepository $productOptionRepository,
  62.         SystemConfigService $systemConfigService,
  63.         Session $session,
  64.         TranslatorInterface $translator
  65.     ) {
  66.         // set params
  67.         $this->productOptionCollectorService $productOptionCollectorService;
  68.         $this->productOptionRepository $productOptionRepository;
  69.         $this->systemConfigService $systemConfigService;
  70.         $this->session $session;
  71.         $this->translator $translator;
  72.     }
  73.     /**
  74.      * {@inheritDoc}
  75.      */
  76.     public static function getSubscribedEvents(): array
  77.     {
  78.         return [
  79.             ProductPageLoadedEvent::class => 'onPageLoaded'
  80.         ];
  81.     }
  82.     /**
  83.      * ...
  84.      *
  85.      * @param ProductPageLoadedEvent $event
  86.      */
  87.     public function onPageLoaded(ProductPageLoadedEvent $event)
  88.     {
  89.         // plugin has to be active
  90.         if ((boolean) $this->systemConfigService->get('DvsnProductOption.config.status'$event->getSalesChannelContext()->getSalesChannel()->getId()) === false) {
  91.             // nothing to do
  92.             return;
  93.         }
  94.         // get params
  95.         $salesChannelContext $event->getSalesChannelContext();
  96.         $product $event->getPage()->getProduct();
  97.         // get the product options
  98.         $options $this->productOptionCollectorService->get(
  99.             $product,
  100.             $salesChannelContext
  101.         )->getElements();
  102.         // is every option hidden
  103.         $allHidden true;
  104.         /** @var ProductOptionEntity $option */
  105.         foreach ($options as $option) {
  106.             $price 0.0;
  107.             switch ($option->getSurchargeType()) {
  108.                 case ProductOptionEntity::SURCHARGE_TYPE_ABSOLUTE:
  109.                     $price $option->getSurcharge();
  110.                     $price $price $salesChannelContext->getCurrency()->getFactor();
  111.                     break;
  112.                 case ProductOptionEntity::SURCHARGE_TYPE_PERCENTAL:
  113.                     $price $product->getCalculatedPrice()->getUnitPrice();
  114.                     $price = ($price 100) * $option->getSurcharge();
  115.                     $price round($price2);
  116.                     break;
  117.             }
  118.             $option->setPrice($price);
  119.             if ($option->getHidden() === false) {
  120.                 $allHidden false;
  121.             }
  122.         }
  123.         // assign to page
  124.         $event->getPage()->assign([
  125.             'dvsnProductOptions' => $options,
  126.             'dvsnProductOptionsHidden' => $allHidden,
  127.             'dvsnProductOptionConfiguration' => $this->systemConfigService->get('DvsnProductOption.config'$salesChannelContext->getSalesChannel()->getId())
  128.         ]);
  129.     }
  130. }