vendor/shopware/core/Checkout/Customer/Subscriber/CustomerDefaultSalutationSubscriber.php line 49

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