custom/plugins/MaxiaTaxSwitch6/src/Subscriber/LoginRegisterSubscriber.php line 61

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