custom/plugins/GrimmTheme/src/Storefront/Subscriber/InstitutionCustomerRegisteredEventSubscriber.php line 35

Open in your IDE?
  1. <?php declare(strict_types 1);
  2. namespace GrimmTheme\Storefront\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use GrimmTheme\Core\Storefront\InstitutionCustomerRegisteredEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\System\SystemConfig\SystemConfigService;
  7. class InstitutionCustomerRegisteredEventSubscriber implements EventSubscriberInterface
  8. {
  9.     protected EntityRepositoryInterface $customerRepository;
  10.     public function __construct(
  11.         EntityRepositoryInterface $customerRepository,
  12.         SystemConfigService $systemConfigService
  13.     )
  14.     {
  15.         $this->customerRepository $customerRepository;
  16.         $this->systemConfigService $systemConfigService;
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             InstitutionCustomerRegisteredEvent::class => 'onInstitutionRegistration'
  22.         ];
  23.     }
  24.     /**
  25.      * Customer group also set on login event. on inition registration and
  26.      * login event, custom field for institution not yet set. This function solves
  27.      * this problem by setting the customer group once upon registration for institution customers
  28.      */
  29.     public function onInstitutionRegistration(InstitutionCustomerRegisteredEvent $event)
  30.     {
  31.         $salesChannelContext $event->getSalesChannelContext();
  32.         $context $salesChannelContext->getContext();
  33.         $customer $event->getCustomer();
  34.         $institutionOptionIsAvailable $this->systemConfigService->get('GrimmTheme.config.showAdditionalCustomerGroupInRegistration') && $this->systemConfigService->get('GrimmTheme.config.additionalCustomerGroup');
  35.         
  36.         if($institutionOptionIsAvailable){
  37.             $customerGroupInstitutionId $this->systemConfigService->get('GrimmTheme.config.additionalCustomerGroup');
  38.         }
  39.         
  40.         if($customerGroupInstitutionId){
  41.             $data = [
  42.                 [
  43.                     'id' => $customer->getId(),
  44.                     'groupId' => $customerGroupInstitutionId,
  45.                 ]
  46.             ];
  47.             $this->customerRepository->update($data$context);
  48.         }
  49.     }
  50. }