src/Core/Subscriber/BusinessEventSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Bodymed\Webshop\Core\Subscriber;
  4. use Bodymed\Webshop\Core\Config\PluginConfig;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity;
  6. use Shopware\Core\Checkout\Order\OrderEntity;
  7. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeSentEvent;
  8. use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class BusinessEventSubscriber implements EventSubscriberInterface
  15. {
  16.     private EntityRepositoryInterface $orderRepository;
  17.     private EntityRepositoryInterface $orderCustomerRepository;
  18.     private PluginConfig $pluginConfig;
  19.     public function __construct(
  20.         EntityRepositoryInterface $orderRepository,
  21.         EntityRepositoryInterface $orderCustomerRepository,
  22.         PluginConfig $pluginConfig
  23.     ) {
  24.         $this->orderRepository $orderRepository;
  25.         $this->orderCustomerRepository $orderCustomerRepository;
  26.         $this->pluginConfig $pluginConfig;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             MailBeforeValidateEvent::class => 'onMailBeforeValidate',
  32.             MailBeforeSentEvent::class => 'onMailBeforeSentEvent'
  33.         ];
  34.     }
  35.     public function onMailBeforeValidate(MailBeforeValidateEvent $mailBeforeValidateEvent): void
  36.     {
  37.         $templateData $mailBeforeValidateEvent->getTemplateData();
  38.         $context $mailBeforeValidateEvent->getContext();
  39.         $data $mailBeforeValidateEvent->getData();
  40.         if (isset($templateData['order'])
  41.             && $templateData['order'] instanceof OrderEntity
  42.         ) {
  43.             $orderId $templateData['order']->getId();
  44.             $criteria = new Criteria([$orderId]);
  45.             $criteria
  46.                 ->addAssociation('deliveries.shippingMethod')
  47.                 ->addAssociation('deliveries.shippingOrderAddress.country')
  48.                 ->addAssociation('transactions.paymentMethod')
  49.                 ->addAssociation('lineItems')
  50.                 ->addAssociation('currency')
  51.                 ->addAssociation('addresses.country');
  52.             /** @var OrderEntity|null $orderEntity */
  53.             $orderEntity $this->orderRepository->search($criteria$context)->first();
  54.             if ($orderEntity) {
  55.                 // todo@dr: can be merged with above criteria after NEXT-4466
  56.                 $orderEntity->setOrderCustomer(
  57.                     $this->fetchCustomer($orderEntity->getId(), $context)
  58.                 );
  59.                 $mailBeforeValidateEvent->addTemplateData('order'$orderEntity);
  60.                 /**
  61.                  * https://madcode.atlassian.net/browse/BODYMED-864
  62.                  *
  63.                  * Der Absendername bei der Shopbetreiber-Benachrichtigung soll geƤndert werden zur E-Mail des Kunden
  64.                  */
  65.                 if (isset($data['templateId']) && isset($data['salesChannelId'])) {
  66.                     if ($data['templateId'] === $this->pluginConfig->getShopOwnerOrderNotificationTemplateId($data['salesChannelId'])) {
  67.                         $data['senderName'] = $orderEntity->getOrderCustomer()->getEmail();
  68.                         $mailBeforeValidateEvent->setData($data);
  69.                     }
  70.                 }
  71.             }
  72.         }
  73.     }
  74.     public function onMailBeforeSentEvent(MailBeforeSentEvent $event): void
  75.     {
  76.         /* Shopowner Mail set ReplyTo */
  77.         $eventData $event->getData();
  78.         if (isset($eventData['templateId']) && isset($eventData['salesChannelId']) && isset($eventData['senderName'])) {
  79.             if ($eventData['templateId'] === $this->pluginConfig->getShopOwnerOrderNotificationTemplateId($eventData['salesChannelId'])) {
  80.                 $event->getMessage()->addReplyTo($eventData['senderName']);
  81.             }
  82.         }
  83.     }
  84.     private function fetchCustomer(string $orderIdContext $context): OrderCustomerEntity
  85.     {
  86.         $criteria = new Criteria();
  87.         $criteria->addFilter(new EqualsFilter('orderId'$orderId));
  88.         $criteria->addAssociation('customer');
  89.         $criteria->addAssociation('salutation');
  90.         return $this->orderCustomerRepository
  91.             ->search($criteria$context)
  92.             ->first();
  93.     }
  94. }