<?php
namespace Twigel\SportLabel\Subscriber;
use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class CustomerSubscriber implements EventSubscriberInterface
{
/**
* @var EntityRepositoryInterface
*/
private EntityRepositoryInterface $customerRepository;
/**
* @var SystemConfigService
*/
private SystemConfigService $config;
/**
* @var EntityRepositoryInterface
*/
private EntityRepositoryInterface $groupRepository;
/**
* @var RequestStack
*/
private RequestStack $request;
public function __construct(
EntityRepositoryInterface $customerRepository,
SystemConfigService $config,
EntityRepositoryInterface $groupRepository,
RequestStack $request
)
{
$this->customerRepository = $customerRepository;
$this->config = $config;
$this->groupRepository = $groupRepository;
$this->request = $request;
}
public static function getSubscribedEvents()
{
return [
CustomerRegisterEvent::class => 'customerRegistry',
GenericPageLoadedEvent::class => 'onPageLoaded'
];
}
public function customerRegistry(CustomerRegisterEvent $event)
{
// if ($this->request->getCurrentRequest()->get('accountType') !== 'client' && $this->request->getCurrentRequest()->get('accountType') !== '') {
// $pluginConfig = $this->config->get('TwigelSportLabel')['config'];
// if (array_key_exists('businessGroup', $pluginConfig) && !is_null($pluginConfig['businessGroup'])) {
// $businessGroupId = $pluginConfig['businessGroup'];
// $event->getCustomer()->setGroupId($businessGroupId);
// $this->customerRepository->update([
// [
// 'id' => $event->getCustomer()->getId(),
// 'groupId' => $businessGroupId
// ]
// ], $event->getContext());
// }
// }
}
public function onPageLoaded(GenericPageLoadedEvent $event){
$pluginConfig = $this->config->get('TwigelSportLabel')['config'];
if (array_key_exists('businessGroup', $pluginConfig)){
$businessGroupId = $pluginConfig['businessGroup'];
$groups = $this->groupRepository->search(new Criteria([$businessGroupId]), $event->getContext())->getElements();
if ($groups){
foreach ($groups as $group){
if ($group->getregistrationTitle()){
$url = $this->request->getCurrentRequest()->get('sw-storefront-url') .'/'. $group->getregistrationTitle();
$event->getPage()->assign(['bussinessUrl' => $url]);
}
}
}
}
}
}