<?php declare(strict_types=1);
namespace ChespackTheme\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Checkout\Customer\CustomerEntity;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use ChespackTheme\Service\ReadingCustomerAgent;
use VioRepresentativeLogin\Core\Services\AgentService;
use Shopware\Core\Framework\Event\DataMappingEvent;
class CustomerSubscriber implements EventSubscriberInterface
{
private ReadingCustomerAgent $readingCustomerAgent;
private AgentService $agentService;
public function __construct(
ReadingCustomerAgent $readingCustomerAgent,
AgentService $agentService
) {
$this->readingCustomerAgent = $readingCustomerAgent;
$this->agentService = $agentService;
}
public static function getSubscribedEvents(): array
{
return [
CustomerEvents::CUSTOMER_LOADED_EVENT => 'onCustomerLoaded',
CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'onCustomerRegister',
];
}
public function onCustomerLoaded(EntityLoadedEvent $event): void
{
/** @var CustomerEntity $customerEntity */
foreach ($event->getEntities() as $customerEntity) {
$agentData = $this->readingCustomerAgent->readAgentData($customerEntity->getUniqueIdentifier());
$customerEntity->addExtension('agent', new ArrayEntity($agentData));
}
}
public function onCustomerRegister(DataMappingEvent $event)
{
// $input = $event->getInput();
$output = $event->getOutput();
$defaultValues = [
"customFields"=>["custom_dimensions_code"=>"NONE"]
];
$output = array_merge($output,$defaultValues);
$event->setOutput($output);
return true;
}
}