custom/plugins/Module23ImprovedTax/src/StoreFrontend/SalesChannel/ImprovedCustomerTax.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Module23ImprovedTax\StoreFrontend\SalesChannel;
  4. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressDefinition;
  5. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  6. use Shopware\Core\Checkout\Customer\CustomerEntity;
  7. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  8. use Shopware\Core\Framework\DataAbstractionLayer\DefinitionInstanceRegistry;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
  12. use function assert;
  13. use function dump;
  14. class ImprovedCustomerTax
  15. {
  16.     private ImprovedCountryTax         $improvedCountryTax;
  17.     private DefinitionInstanceRegistry $definitionRegistry;
  18.     /**
  19.      * @param ImprovedCountryTax $improvedCountryTax
  20.      */
  21.     public function __construct(ImprovedCountryTax $improvedCountryTax, DefinitionInstanceRegistry $definitionRegistry)
  22.     {
  23.         $this->improvedCountryTax = $improvedCountryTax;
  24.         $this->definitionRegistry = $definitionRegistry;
  25.     }
  26.     public function __invoke(EntityLoadedEvent $event): void
  27.     {
  28.         if (empty($event->getEntities()) || ($event->getContext()->getSource() instanceof AdminApiSource)) {
  29.             return;
  30.         }
  31.         $customer = $event->getEntities()[0];
  32.         assert($customer instanceof CustomerEntity);
  33.         $definition = $this->definitionRegistry->get(CustomerAddressDefinition::class);
  34.         $repository = $this->definitionRegistry->getRepository($definition->getEntityName());
  35.         $result  = $repository->search(new Criteria([$customer->getDefaultBillingAddressId()]), $event->getContext());
  36.         $address = $result->first();
  37.         assert($address instanceof CustomerAddressEntity);
  38.         $customer->setDefaultBillingAddress($address);
  39.         $this->improvedCountryTax->setCustomer($customer);
  40.     }
  41. }