custom/plugins/ChespackTheme/src/Subscriber/CustomerSubscriber.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ChespackTheme\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  4. use Shopware\Core\Framework\Struct\ArrayEntity;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Shopware\Core\Checkout\Customer\CustomerEntity;
  7. use Shopware\Core\Checkout\Customer\CustomerEvents;
  8. use ChespackTheme\Service\ReadingCustomerAgent;
  9. use VioRepresentativeLogin\Core\Services\AgentService;
  10. use Shopware\Core\Framework\Event\DataMappingEvent;
  11. class CustomerSubscriber implements EventSubscriberInterface
  12. {
  13.     private ReadingCustomerAgent $readingCustomerAgent;
  14.     private AgentService $agentService;
  15.     public function __construct(
  16.         ReadingCustomerAgent $readingCustomerAgent,
  17.         AgentService $agentService
  18.     ) {
  19.         $this->readingCustomerAgent $readingCustomerAgent;
  20.         $this->agentService $agentService;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             CustomerEvents::CUSTOMER_LOADED_EVENT => 'onCustomerLoaded',
  26.             CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'onCustomerRegister',
  27.         ];
  28.     }
  29.     public function onCustomerLoaded(EntityLoadedEvent $event): void
  30.     {
  31.         /** @var CustomerEntity $customerEntity */
  32.         foreach ($event->getEntities() as $customerEntity) {
  33.             $agentData $this->readingCustomerAgent->readAgentData($customerEntity->getUniqueIdentifier());
  34.             $customerEntity->addExtension('agent', new ArrayEntity($agentData));
  35.         }
  36.     }
  37.     public function onCustomerRegister(DataMappingEvent $event)
  38.     {
  39.         // $input = $event->getInput();
  40.         $output $event->getOutput();
  41.         $defaultValues = [
  42.             "customFields"=>["custom_dimensions_code"=>"NONE"]
  43.         ];
  44.         $output array_merge($output,$defaultValues);
  45.         $event->setOutput($output);
  46.         return true;
  47.     }
  48. }