<?php declare(strict_types=1);
namespace Wingsacademy\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\Event\DataMappingEvent;
use Shopware\Core\Checkout\Customer\CustomerEvents;
use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
class MySubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
CustomerEvents::MAPPING_REGISTER_CUSTOMER => 'addEmail',
CustomerEvents::MAPPING_ADDRESS_CREATE => 'addEmail',
CustomerEvents::MAPPING_REGISTER_ADDRESS_SHIPPING => 'addEmail',
CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => 'onRegisterBillingAddress',
// CustomerEvents::MAPPING_CUSTOMER_PROFILE_SAVE => 'onRegisterBillingAddress',
ProductListingCriteriaEvent::class => 'handleListingRequest',
ProductSearchCriteriaEvent::class => 'handleSearchListingRequest'
];
}
public function addEmail(DataMappingEvent $event) {
$data = $event->getInput();
$address = $event->getOutput();
$address['customFields']['custom_adress_email'] = $data->get('AddressEmail');
$event->setOutput($address);
}
public function handleListingRequest(ProductListingCriteriaEvent $event): void {
$event->getCriteria()->addAssociation('properties');
$event->getCriteria()->addAssociation('properties.group');
}
public function handleSearchListingRequest(ProductSearchCriteriaEvent $event): void {
$event->getCriteria()->addAssociation('properties');
$event->getCriteria()->addAssociation('properties.group');
}
public function onRegisterBillingAddress(DataMappingEvent $event) {
$data = $event->getInput();
$address = $event->getOutput();
if($data->has('customFields')) {
$customFieldKeys = [
"custom_adress_salutation",
"custom_adress_firstName",
"custom_adress_lastName",
"custom_adress_company",
"custom_adress_department",
"custom_adress_invoiceEmail",
"custom_adress_plz",
"custom_adress_city",
"custom_adress_additionalAddressLine1",
"custom_adress_countryId",
"custom_adress_number",
"custom_adress_mobile_phone_billing",
"custom_adress_mobile_phone_registration",
"custom_adress_street",
"custom_adress_vatId"
];
foreach($customFieldKeys as $key) {
if($data->get('customFields')->has($key)) {
$address["customFields"][$key] = $data->get('customFields')->get($key);
}
}
}
$address['customFields']['custom_adress_email'] = $data->get('AddressEmail');
$event->setOutput($address);
}
}