custom/plugins/Wingsacademy/src/Subscriber/MySubscriber.php line 32

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Wingsacademy\Subscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Shopware\Core\Framework\Event\DataMappingEvent;
  5. use Shopware\Core\Checkout\Customer\CustomerEvents;
  6. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  7. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  8. class MySubscriber implements EventSubscriberInterface
  9. {   
  10.     public static function getSubscribedEvents(): array
  11.     {
  12.         return [
  13.             CustomerEvents::MAPPING_REGISTER_CUSTOMER  => 'addEmail',
  14.             CustomerEvents::MAPPING_ADDRESS_CREATE => 'addEmail',
  15.             CustomerEvents::MAPPING_REGISTER_ADDRESS_SHIPPING => 'addEmail',
  16.             CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => 'onRegisterBillingAddress',
  17.             // CustomerEvents::MAPPING_CUSTOMER_PROFILE_SAVE => 'onRegisterBillingAddress',
  18.             ProductListingCriteriaEvent::class => 'handleListingRequest',
  19.             ProductSearchCriteriaEvent::class => 'handleSearchListingRequest'
  20.         ];
  21.     }
  22.     public function addEmail(DataMappingEvent $event) {
  23.         $data $event->getInput();
  24.         $address $event->getOutput();
  25.         $address['customFields']['custom_adress_email'] = $data->get('AddressEmail');
  26.         $event->setOutput($address);
  27.     }
  28.     public function handleListingRequest(ProductListingCriteriaEvent $event): void {
  29.         $event->getCriteria()->addAssociation('properties');
  30.         $event->getCriteria()->addAssociation('properties.group');
  31.     }
  32.     public function handleSearchListingRequest(ProductSearchCriteriaEvent $event): void {
  33.         $event->getCriteria()->addAssociation('properties');
  34.         $event->getCriteria()->addAssociation('properties.group');
  35.     }
  36.     public function onRegisterBillingAddress(DataMappingEvent $event) {
  37.         $data $event->getInput();
  38.         $address $event->getOutput();
  39.         if($data->has('customFields')) {
  40.             $customFieldKeys = [
  41.                 "custom_adress_salutation",
  42.                 "custom_adress_firstName",
  43.                 "custom_adress_lastName",
  44.                 "custom_adress_company",
  45.                 "custom_adress_department",
  46.                 "custom_adress_invoiceEmail",
  47.                 "custom_adress_plz",
  48.                 "custom_adress_city",
  49.                 "custom_adress_additionalAddressLine1",
  50.                 "custom_adress_countryId",
  51.                 "custom_adress_number",
  52.                 "custom_adress_mobile_phone_billing",
  53.                 "custom_adress_mobile_phone_registration",
  54.                 "custom_adress_street",
  55.                 "custom_adress_vatId"
  56.             ];
  57.             
  58.             foreach($customFieldKeys as $key) {
  59.                 if($data->get('customFields')->has($key)) {
  60.                     $address["customFields"][$key] = $data->get('customFields')->get($key);
  61.                 }
  62.             }
  63.         }
  64.         $address['customFields']['custom_adress_email'] = $data->get('AddressEmail');
  65.         $event->setOutput($address);
  66.     }
  67. }