<?php
declare(strict_types=1);
namespace Bodymed\Webshop\Core\Subscriber;
use Bodymed\Webshop\Core\Config\PluginConfig;
use Shopware\Core\Checkout\Order\Aggregate\OrderCustomer\OrderCustomerEntity;
use Shopware\Core\Checkout\Order\OrderEntity;
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeSentEvent;
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class BusinessEventSubscriber implements EventSubscriberInterface
{
private EntityRepositoryInterface $orderRepository;
private EntityRepositoryInterface $orderCustomerRepository;
private PluginConfig $pluginConfig;
public function __construct(
EntityRepositoryInterface $orderRepository,
EntityRepositoryInterface $orderCustomerRepository,
PluginConfig $pluginConfig
) {
$this->orderRepository = $orderRepository;
$this->orderCustomerRepository = $orderCustomerRepository;
$this->pluginConfig = $pluginConfig;
}
public static function getSubscribedEvents(): array
{
return [
MailBeforeValidateEvent::class => 'onMailBeforeValidate',
MailBeforeSentEvent::class => 'onMailBeforeSentEvent'
];
}
public function onMailBeforeValidate(MailBeforeValidateEvent $mailBeforeValidateEvent): void
{
$templateData = $mailBeforeValidateEvent->getTemplateData();
$context = $mailBeforeValidateEvent->getContext();
$data = $mailBeforeValidateEvent->getData();
if (isset($templateData['order'])
&& $templateData['order'] instanceof OrderEntity
) {
$orderId = $templateData['order']->getId();
$criteria = new Criteria([$orderId]);
$criteria
->addAssociation('deliveries.shippingMethod')
->addAssociation('deliveries.shippingOrderAddress.country')
->addAssociation('transactions.paymentMethod')
->addAssociation('lineItems')
->addAssociation('currency')
->addAssociation('addresses.country');
/** @var OrderEntity|null $orderEntity */
$orderEntity = $this->orderRepository->search($criteria, $context)->first();
if ($orderEntity) {
// todo@dr: can be merged with above criteria after NEXT-4466
$orderEntity->setOrderCustomer(
$this->fetchCustomer($orderEntity->getId(), $context)
);
$mailBeforeValidateEvent->addTemplateData('order', $orderEntity);
/**
* https://madcode.atlassian.net/browse/BODYMED-864
*
* Der Absendername bei der Shopbetreiber-Benachrichtigung soll geƤndert werden zur E-Mail des Kunden
*/
if (isset($data['templateId']) && isset($data['salesChannelId'])) {
if ($data['templateId'] === $this->pluginConfig->getShopOwnerOrderNotificationTemplateId($data['salesChannelId'])) {
$data['senderName'] = $orderEntity->getOrderCustomer()->getEmail();
$mailBeforeValidateEvent->setData($data);
}
}
}
}
}
public function onMailBeforeSentEvent(MailBeforeSentEvent $event): void
{
/* Shopowner Mail set ReplyTo */
$eventData = $event->getData();
if (isset($eventData['templateId']) && isset($eventData['salesChannelId']) && isset($eventData['senderName'])) {
if ($eventData['templateId'] === $this->pluginConfig->getShopOwnerOrderNotificationTemplateId($eventData['salesChannelId'])) {
$event->getMessage()->addReplyTo($eventData['senderName']);
}
}
}
private function fetchCustomer(string $orderId, Context $context): OrderCustomerEntity
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('orderId', $orderId));
$criteria->addAssociation('customer');
$criteria->addAssociation('salutation');
return $this->orderCustomerRepository
->search($criteria, $context)
->first();
}
}