custom/plugins/MadNoms/src/Subscriber/CustomerEventsSubscriber.php line 45

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MadNoms\Subscriber;
  3. use Shopware\Core\Checkout\Customer\CustomerEvents;
  4. //use Shopware\Core\Checkout\Customer\Event\CustomerRegisterEvent;
  5. use Shopware\Storefront\Controller\AddressController;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\Framework\Event\DataMappingEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. class CustomerEventsSubscriber implements EventSubscriberInterface
  13. {
  14.     private EntityRepositoryInterface $nomsCustomerAddressRepository;
  15.     private EntityRepositoryInterface $addressRepository;
  16.     public function __construct(
  17.         EntityRepositoryInterface $nomsCustomerAddressRepository,
  18.         EntityRepositoryInterface $addressRepository
  19.     )
  20.     {
  21.         $this->nomsCustomerAddressRepository $nomsCustomerAddressRepository;
  22.         $this->addressRepository $addressRepository;
  23.     }
  24.     public static function getSubscribedEvents()
  25.     {
  26.         return [
  27.             //CustomerRegisterEvent::class => 'onCreateCustomer',
  28.             AddressController::class => 'onSaveAddress',
  29.             CustomerEvents::MAPPING_ADDRESS_CREATE => 'onMappingAddress',
  30.             CustomerEvents::MAPPING_REGISTER_ADDRESS_SHIPPING => 'onMappingAddress',
  31.             CustomerEvents::MAPPING_REGISTER_ADDRESS_BILLING => 'onMappingAddress',
  32.             CustomerEvents::CUSTOMER_ADDRESS_LOADED_EVENT => 'onCustomerAddressLoaded'
  33.         ];
  34.     }
  35.     public function onCustomerAddressLoaded(EntityLoadedEvent $event): void
  36.     {
  37.         $context $event->getContext();
  38.         $addresses $event->getEntities();
  39.         foreach($addresses as $address){
  40.             $id $address->getId();
  41.             $criteria = (new Criteria())->addFilter(
  42.                 new EqualsFilter('customerAddressId'$id),
  43.             );
  44.             $result $this->nomsCustomerAddressRepository->search($criteria$context);
  45.             $nomsCustomerAddress $result->first();
  46.             $address->addExtension(
  47.                 'nomsCustomerAddress',
  48.                 $nomsCustomerAddress
  49.             );
  50.             if($address->getCustomFields()){
  51.                 if(isset($address->getCustomFields()['nomsStreet']) and $address->getExtensions()['nomsCustomerAddress'] == null){
  52.                     $this->nomsCustomerAddressRepository->create([
  53.                         [
  54.                             'customerAddressId' => $address->getId(),
  55.                             'customerId' => $address->getCustomerId(),
  56.                             'street' => $address->getCustomFields()['nomsStreet'],
  57.                             'streetnumber' => $address->getCustomFields()['nomsStreetnumber']
  58.                         ]
  59.                     ], $context);
  60.                     $criteria = (new Criteria())->addFilter(
  61.                         new EqualsFilter('customerAddressId'$id),
  62.                     );
  63.                     $result $this->nomsCustomerAddressRepository->search($criteria$context);
  64.                     $nomsCustomerAddress $result->first();
  65.                     $address->addExtension(
  66.                         'nomsCustomerAddress',
  67.                         $nomsCustomerAddress
  68.                     );
  69.                 }
  70.             }
  71.         }
  72.     }
  73.     /*public function onCreateCustomer(CustomerRegisterEvent $event): void
  74.     {
  75.             $context = $event->getSalesChannelContext()->getContext();
  76.             $street = $event->getCustomer()->getDefaultBillingAddress()->getCustomFields()['nomsStreet'];
  77.             $streetnumber = $event->getCustomer()->getDefaultBillingAddress()->getCustomFields()['nomsStreetnumber'];
  78.             $customerId = $event->getCustomer()->getId();
  79.             $customerBillingAddressId = $event->getCustomer()->getDefaultBillingAddress()->getId();
  80.             $customerShippingAddressId = $event->getCustomer()->getDefaultShippingAddress()->getId();
  81.             $this->nomsCustomerAddressRepository->create([
  82.                 [
  83.                     'customerAddressId' => $customerBillingAddressId,
  84.                     'customerId' => $customerId,
  85.                     'street' => $street,
  86.                     'streetnumber' => $streetnumber
  87.                 ]
  88.             ], $context);
  89.             if ($customerBillingAddressId != $customerShippingAddressId) {
  90.                 $street = $event->getCustomer()->getDefaultShippingAddress()->getCustomFields()['nomsStreet'];
  91.                 $streetnumber = $event->getCustomer()->getDefaultShippingAddress()->getCustomFields()['nomsStreetnumber'];
  92.                 $this->nomsCustomerAddressRepository->create([
  93.                     [
  94.                         'customerAddressId' => $customerShippingAddressId,
  95.                         'customerId' => $customerId,
  96.                         'street' => $street,
  97.                         'streetnumber' => $streetnumber
  98.                     ]
  99.                 ], $context);
  100.             }
  101.     }*/
  102.     public function onMappingAddress(DataMappingEvent $event): void
  103.     {
  104.         $data $event->getInput();
  105.         $address $event->getOutput();
  106.         //Set Street and Streetnumber in shopware orginal field
  107.         $address['customFields']['nomsStreet'] =  $address['street'];
  108.         $address['customFields']['nomsStreetnumber'] =  $data->get('streetnumber');
  109.         $address['street'] = $address['street'] .' '$data->get('streetnumber');
  110.         if($data->get('id')){
  111.             $criteria = (new Criteria())->addFilter(
  112.                 new EqualsFilter('customerAddressId'$data->get('id')),
  113.             );
  114.             $nomsCustomerAddressId $this->nomsCustomerAddressRepository->searchIds($criteria$event->getContext())->firstId();
  115.             if($nomsCustomerAddressId){
  116.                 $this->nomsCustomerAddressRepository->update([
  117.                     [
  118.                         'id' => $nomsCustomerAddressId,
  119.                         'street' => $address['customFields']['nomsStreet'],
  120.                         'streetnumber' => $address['customFields']['nomsStreetnumber']
  121.                     ]
  122.                 ], $event->getContext());
  123.             }else{
  124.                 $this->nomsCustomerAddressRepository->create([
  125.                     [
  126.                         'customerAddressId' => $data->get('id'),
  127.                         'customerId' => $data->get('nomsId'),
  128.                         'street' => $address['customFields']['nomsStreet'],
  129.                         'streetnumber' => $address['customFields']['nomsStreetnumber']
  130.                     ]
  131.                 ], $event->getContext());
  132.             }
  133.         }
  134.         $event->setOutput($address);
  135.     }
  136. }