<?php
declare(strict_types=1);
namespace S360\TaxSwitch\Storefront\Subscriber;
use S360\TaxSwitch\Service\TaxStateHelper;
use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CustomerLoginEventSubscriber implements EventSubscriberInterface
{
/**
* @var TaxStateHelper
*/
protected TaxStateHelper $taxStateHelper;
/**
* CustomerLoginEventSubscriber constructor.
*/
public function __construct(TaxStateHelper $taxStateHelper)
{
$this->taxStateHelper = $taxStateHelper;
}
public static function getSubscribedEvents(): array
{
return [
CustomerLoginEvent::class => 'persistTaxChoice'
];
}
/**
* @param CustomerLoginEvent $event
*/
public function persistTaxChoice(CustomerLoginEvent $event): void
{
$context = $event->getContext();
$customer = $event->getCustomer();
$isGross = true;
$company = null;
if($customer){
$company = $customer->getCompany();
}
// if registered with company, company name returned, else null
if(isset($company)){
$isGross = false;
}
$this->taxStateHelper->persistTaxChoiceToCustomer($context, $customer, $isGross);
$this->taxStateHelper->persistTaxChoiceToCookie($isGross);
}
}