custom/plugins/MaxiaTaxSwitch6/src/Storefront/Subscriber/LoginRegisterSubscriber.php line 59

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Maxia\MaxiaTaxSwitch6\Storefront\Subscriber;
  3. use Maxia\MaxiaTaxSwitch6\Storefront\Events\CustomerGroupAssignedEvent;
  4. use Maxia\MaxiaTaxSwitch6\Storefront\Service\TaxSwitchService;
  5. use Shopware\Core\Checkout\Customer\Aggregate\CustomerGroup\CustomerGroupEntity;
  6. use Shopware\Core\Checkout\Customer\CustomerEntity;
  7. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  8. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  9. use Shopware\Core\Checkout\Customer\Event\GuestCustomerRegisterEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. /**
  17.  * @package Maxia\MaxiaAdvBlockPrices6\Subscriber
  18.  */
  19. class LoginRegisterSubscriber implements EventSubscriberInterface
  20. {
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             CustomerLoginEvent::class => 'setTaxSettingOnLogin',
  25.             CustomerRegisterEvent::class => 'setCustomerGroupOnRegister',
  26.             GuestCustomerRegisterEvent::class => 'setGuestCustomerGroupOnRegister'
  27.         ];
  28.     }
  29.     /** @var EventDispatcherInterface */
  30.     private $eventDispatcher;
  31.     /** @var EntityRepositoryInterface */
  32.     private $customerGroupRepository;
  33.     /** @var EntityRepositoryInterface */
  34.     private $customerRepository;
  35.     /** @var TaxSwitchService */
  36.     private $taxSwitchService;
  37.     public function __construct(
  38.         RequestStack $requestStack,
  39.         EventDispatcherInterface $eventDispatcher,
  40.         EntityRepositoryInterface $customerGroupRepository,
  41.         EntityRepositoryInterface $customerRepository,
  42.         TaxSwitchService $taxSwitchService
  43.     ) {
  44.         $this->requestStack $requestStack;
  45.         $this->eventDispatcher $eventDispatcher;
  46.         $this->customerGroupRepository $customerGroupRepository;
  47.         $this->customerRepository $customerRepository;
  48.         $this->taxSwitchService $taxSwitchService;
  49.     }
  50.     public function setTaxSettingOnLogin(CustomerLoginEvent $event)
  51.     {
  52.         $context $event->getSalesChannelContext();
  53.         $config $this->taxSwitchService->getConfig($context);
  54.         if (!$config['pluginEnabled'] || $this->isApiRequest()) {
  55.             return;
  56.         }
  57.         if ($config['updateSettingOnLogin']) {
  58.             // get the actual 'displayGross' setting from DB
  59.             /** @var CustomerGroupEntity $group */
  60.             $group $this->customerGroupRepository->search(
  61.                 new Criteria([$event->getCustomer()->getGroupId()]),
  62.                 $context->getContext()
  63.             )->first();
  64.             $this->taxSwitchService->setDisplayNet($event->getSalesChannelContext(), !$group->getDisplayGross());
  65.         }
  66.     }
  67.     public function setCustomerGroupOnRegister(CustomerRegisterEvent $event)
  68.     {
  69.         $this->handleRegistration($event->getCustomer(), $event->getSalesChannelContext());
  70.     }
  71.     public function setGuestCustomerGroupOnRegister(GuestCustomerRegisterEvent $event)
  72.     {
  73.         $this->handleRegistration($event->getCustomer(), $event->getSalesChannelContext());
  74.     }
  75.     protected function handleRegistration(CustomerEntity $customerSalesChannelContext $context)
  76.     {
  77.         $config $this->taxSwitchService->getConfig($context);
  78.         if (!$config['pluginEnabled'] || !$config['groupRegistrationMode']
  79.             || $config['groupRegistrationMode'] === 'disabled'
  80.             || $this->isApiRequest()
  81.         ) {
  82.             return;
  83.         }
  84.         // check if customer has filled the company field
  85.         $hasEnteredCompany false;
  86.         foreach ($customer->getAddresses() as $address) {
  87.             if ($address->getCompany()) {
  88.                 $hasEnteredCompany true;
  89.                 break;
  90.             }
  91.         }
  92.         $groupId $hasEnteredCompany $config['businessCustomerGroup'] : $config['privateCustomerGroup'];
  93.         if ($config['groupRegistrationMode'] === 'assign') {
  94.             $this->updateGroup($customer,  $groupId$context);
  95.         } else if ($config['groupRegistrationMode'] === 'request') {
  96.             $this->updateRequestedGroup($customer$groupId$context);
  97.         }
  98.     }
  99.     protected function updateGroup(CustomerEntity $customer$groupIdSalesChannelContext $context)
  100.     {
  101.         // fetch the group to assign
  102.         /** @var CustomerGroupEntity $group */
  103.         $group $this->customerGroupRepository->search(
  104.             new Criteria([$groupId]), $context->getContext()
  105.         )->first();
  106.         if (!$group || $groupId === $customer->getGroupId()) {
  107.             return;
  108.         }
  109.         // assign customer group
  110.         $customer->setGroupId($group->getId());
  111.         $customer->setGroup($group);
  112.         $this->customerRepository->update([[
  113.             'id' => $customer->getId(),
  114.             'groupId' => $group->getId()
  115.         ]], $context->getContext());
  116.         // update tax switch setting for new group
  117.         $groupIsNet = !$group->getDisplayGross();
  118.         if ($groupIsNet !== $this->taxSwitchService->getDisplayNet($context)) {
  119.             $this->taxSwitchService->setDisplayNet($context$groupIsNet);
  120.         }
  121.         $this->eventDispatcher->dispatch(new CustomerGroupAssignedEvent($customer$group$groupIsNet$context));
  122.     }
  123.     protected function updateRequestedGroup(CustomerEntity $customer$groupIdSalesChannelContext $context)
  124.     {
  125.         if (!method_exists($customer'getRequestedGroupId')
  126.             || $customer->getRequestedGroupId())
  127.         {
  128.             // request group not supported or already set
  129.             return;
  130.         }
  131.         if ($customer->getGroupId() !== $groupId) {
  132.             $customer->setRequestedGroupId($groupId);
  133.             $this->customerRepository->update([[
  134.                 'id' => $customer->getId(),
  135.                 'requestedGroupId' => $groupId
  136.             ]], $context->getContext());
  137.         }
  138.     }
  139.     /**
  140.      * @return \Symfony\Component\HttpFoundation\Request|null
  141.      */
  142.     protected function getMainRequest()
  143.     {
  144.         return method_exists($this->requestStack'getMainRequest')
  145.             ? $this->requestStack->getMainRequest()
  146.             : $this->requestStack->getMasterRequest();
  147.     }
  148.     /**
  149.      * Checks if the current request is a Store-API request.
  150.      */
  151.     protected function isApiRequest(): bool
  152.     {
  153.         return $this->getMainRequest() &&
  154.             (str_starts_with($this->getMainRequest()->getPathInfo(), '/api/_proxy/store-api/') ||
  155.                 str_starts_with($this->getMainRequest()->getPathInfo(), '/api/_proxy-order/') ||
  156.                 str_starts_with($this->getMainRequest()->getPathInfo(), '/store-api/'));
  157.     }
  158. }