custom/static-plugins/SamsonCustomer/src/Subscriber/CustomerDefaultSalutationSubscriber.php line 60

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Samson\Subscriber;
  3. /***
  4.  *
  5.  * This file is part of the "SAMSON Shop" project.
  6.  *
  7.  * For the full copyright and license information, please read the
  8.  * LICENSE.txt file that was distributed with this source code.
  9.  *
  10.  *  (c) 2022
  11.  *
  12.  ***/
  13. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressEntity;
  14. use Shopware\Core\Checkout\Customer\CustomerEntity;
  15. use Shopware\Core\Checkout\Customer\CustomerEvents;
  16. use Shopware\Core\Checkout\Order\Aggregate\OrderAddress\OrderAddressEntity;
  17. use Shopware\Core\Checkout\Order\OrderEvents;
  18. use Shopware\Core\Defaults;
  19. use Shopware\Core\Framework\Context;
  20. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  22. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  23. use Shopware\Core\Framework\Feature;
  24. use Shopware\Core\System\Salutation\SalutationEntity;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. class CustomerDefaultSalutationSubscriber implements EventSubscriberInterface
  27. {
  28.     private EntityRepositoryInterface $salutationRepository;
  29.     private ?SalutationEntity $defaultSalutation null;
  30.     public function __construct(EntityRepositoryInterface $salutationRepository)
  31.     {
  32.         $this->salutationRepository $salutationRepository;
  33.     }
  34.     public static function getSubscribedEvents(): array
  35.     {
  36.         if (Feature::isActive('FEATURE_NEXT_7739')) {
  37.             return [];
  38.         }
  39.         return [
  40.             CustomerEvents::CUSTOMER_LOADED_EVENT => [
  41.                 ['loaded'],
  42.             ],
  43.             CustomerEvents::CUSTOMER_ADDRESS_LOADED_EVENT => [
  44.                 ['loaded'],
  45.             ],
  46.             OrderEvents::ORDER_ADDRESS_LOADED_EVENT => [
  47.                 ['loaded'],
  48.             ],
  49.         ];
  50.     }
  51.     public function loaded(EntityLoadedEvent $event): void
  52.     {
  53.         /** @var CustomerEntity|CustomerAddressEntity|OrderAddressEntity $entity */
  54.         foreach ($event->getEntities() as $entity) {
  55.             if ($entity->getSalutationId() === null) {
  56.                 $entity->setSalutationId($this->getDefaultSalutation($event->getContext())->getId());
  57.             }
  58.         }
  59.     }
  60.     private function getDefaultSalutation(Context $context): SalutationEntity
  61.     {
  62.         if ($this->defaultSalutation !== null) {
  63.             return $this->defaultSalutation;
  64.         }
  65.         $this->defaultSalutation $this->salutationRepository->search(
  66.             new Criteria([Defaults::SALUTATION]),
  67.             $context
  68.         )->first();
  69.         return $this->defaultSalutation;
  70.     }
  71. }