<?php declare(strict_types = 1);
namespace GrimmTheme\Storefront\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use GrimmTheme\Core\Storefront\InstitutionCustomerRegisteredEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;
class InstitutionCustomerRegisteredEventSubscriber implements EventSubscriberInterface
{
protected EntityRepositoryInterface $customerRepository;
public function __construct(
EntityRepositoryInterface $customerRepository,
SystemConfigService $systemConfigService
)
{
$this->customerRepository = $customerRepository;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array
{
return [
InstitutionCustomerRegisteredEvent::class => 'onInstitutionRegistration'
];
}
/**
* Customer group also set on login event. on inition registration and
* login event, custom field for institution not yet set. This function solves
* this problem by setting the customer group once upon registration for institution customers
*/
public function onInstitutionRegistration(InstitutionCustomerRegisteredEvent $event)
{
$salesChannelContext = $event->getSalesChannelContext();
$context = $salesChannelContext->getContext();
$customer = $event->getCustomer();
$institutionOptionIsAvailable = $this->systemConfigService->get('GrimmTheme.config.showAdditionalCustomerGroupInRegistration') && $this->systemConfigService->get('GrimmTheme.config.additionalCustomerGroup');
if($institutionOptionIsAvailable){
$customerGroupInstitutionId = $this->systemConfigService->get('GrimmTheme.config.additionalCustomerGroup');
}
if($customerGroupInstitutionId){
$data = [
[
'id' => $customer->getId(),
'groupId' => $customerGroupInstitutionId,
]
];
$this->customerRepository->update($data, $context);
}
}
}