custom/plugins/GrimmTheme/src/Storefront/Subscriber/RegistrationSubscriber.php line 36

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace GrimmTheme\Storefront\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  5. use Shopware\Core\Checkout\Customer\Event\GuestCustomerRegisterEvent;
  6. use GrimmTheme\Service\TaxIdValidator;
  7. class RegistrationSubscriber implements EventSubscriberInterface
  8. {
  9.     /**
  10.      * @var TaxIdValidator
  11.      */
  12.     private $taxIdValidator;
  13.     public function __construct(
  14.         TaxIdValidator $taxIdValidator
  15.     ){
  16.         $this->taxIdValidator $taxIdValidator;
  17.     }
  18.     
  19.     // for both customer and guest registration, same function executed since guestr registration event extends customer registration event
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             CustomerRegisterEvent::class => 'onRegisterEvent',
  24.             GuestCustomerRegisterEvent::class => 'onRegisterEvent'
  25.         ];
  26.     }
  27.     /**
  28.      * validates vat against endpoints and updates custom field of customer entity if valid
  29.      * sets boolean custom_grimm_customer_tax_free which determines in the TaxDetector, if customer gets VAT free order
  30.      */
  31.     public function onRegisterEvent(CustomerRegisterEvent $event)
  32.     {
  33.         $customer $event->getCustomer();
  34.         $context $event->getSalesChannelContext();
  35.         $billingAddress $customer->getDefaultBillingAddress();
  36.         $billingAddressCountry $billingAddress->getCountry();
  37.         $countryIso $billingAddressCountry->getIso();
  38.         $countryCompanyTaxFree $billingAddressCountry->getCompanyTax()->getEnabled();
  39.         if ($countryIso == "DE" || !$countryCompanyTaxFree || !$customer->getCompany()) {
  40.             return;
  41.         }
  42.         $vatIds $customer->getVatIds();
  43.         if($vatIds && isset($vatIds[0])){
  44.             $vatIds $vatIds[0];
  45.             $vatIdValid $this->taxIdValidator->validateForeignVAT($vatIds$billingAddress);
  46.             
  47.             if($vatIdValid){
  48.                 $this->taxIdValidator->setVatValidCustomField($customer$vatIdValid$context);
  49.             }
  50.         }
  51.     }
  52. }