custom/plugins/HemoLauterRegistry/src/Subscriber/RegistrationSubscriber.php line 75

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace HemoLauter\Subscriber;
  3. use HemoLauter\Util\Constant;
  4. use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupEntity;
  5. use Shopware\Core\Checkout\Customer\CustomerEvents;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\Event\DataMappingEvent;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Storefront\Page\Account\Login\AccountLoginPageLoadedEvent;
  11. use Shopware\Storefront\Page\Account\Overview\AccountOverviewPageLoadedEvent;
  12. use Shopware\Storefront\Page\Checkout\Register\CheckoutRegisterPageLoadedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class RegistrationSubscriber implements EventSubscriberInterface
  15. {
  16.     private EntityRepository $repository;
  17.     private SystemConfigService $systemConfigService;
  18.     public function __construct(EntityRepository $repositorySystemConfigService $systemConfigService)
  19.     {
  20.         $this->repository $repository;
  21.         $this->systemConfigService $systemConfigService;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             AccountLoginPageLoadedEvent::class => 'onPageLoaded',
  27.             AccountOverviewPageLoadedEvent::class => 'onPageOverviewLoaded',
  28.             CheckoutRegisterPageLoadedEvent::class => 'onPageLoaded',
  29.             CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'onCustomerRegister',
  30.         ];
  31.     }
  32.     public function onCustomerRegister(DataMappingEvent $event): void
  33.     {
  34.         $data $event->getInput();
  35.         $group $this->systemConfigService->get('HemoLauterRegistry.config.customerGroup');
  36.         $id $data->get('groupId');
  37.         if ($id && in_array($id$group)) {
  38.             $output $event->getOutput();
  39.             $output['groupId'] = $id;
  40.             $event->setOutput($output);
  41.         }
  42.     }
  43.     public function onPageLoaded($event): void
  44.     {
  45.         $this->getCustomerGroup($event);
  46.     }
  47.     public function onPageOverviewLoaded($event): void
  48.     {
  49.         $this->getCustomerGroup($event);
  50.     }
  51.     /**
  52.      * @param  $event
  53.      * @return void
  54.      */
  55.     public function getCustomerGroup($event): void
  56.     {
  57.         $page $event->getContext();
  58.         $criteria = new Criteria();
  59.         $customerGroups $this->repository->search($criteria$event->getContext());
  60.         $group $this->systemConfigService->get('HemoLauterRegistry.config.customerGroup');
  61.         $page->assign([Constant::CUSTOMER_GROUPS => $customerGroups]);
  62.         $page->assign([Constant::FIELD_CONFIG => $group]);
  63.     }
  64. }