From ea25aa47c12a07e041106825730981d9d29cd20d Mon Sep 17 00:00:00 2001 From: Hung Mac Date: Thu, 26 Nov 2020 21:01:22 +0700 Subject: [PATCH] Fixed URL of mail template --- CHANGELOG_de-DE.md | 4 + CHANGELOG_en-GB.md | 4 + composer.json | 2 +- .../Cart/Subscriber/OrderPlacedSubscriber.php | 1 + .../Migration1606395595FixUrlMailTemplate.php | 72 ++++++++++++++++ .../de/download-html-mail-template.html.twig | 2 +- .../de/download-plain-mail-template.html.twig | 2 +- .../download-html-mail-template.html.twig | 2 +- .../download-plain-mail-template.html.twig | 2 +- src/Service/EsdOrderService.php | 86 +++++++++++++++---- .../OrderStateChangedSubscriber.php | 15 +++- 11 files changed, 166 insertions(+), 26 deletions(-) create mode 100644 src/Migration/Migration1606395595FixUrlMailTemplate.php diff --git a/CHANGELOG_de-DE.md b/CHANGELOG_de-DE.md index 1450854..53e1f39 100644 --- a/CHANGELOG_de-DE.md +++ b/CHANGELOG_de-DE.md @@ -1,3 +1,7 @@ +# 1.2.10 +* Die url in der E-Mail-Vorlage wurde repariert, Wechsel von url() zu rawUrl(), um die Vertriebskanal-Domain zu erhalten +* Verbesserte Funktion zum Versenden von E-Mails, Sie können die esd-E-Mail an den Käufer senden Kaufen Ändern Sie den Zahlungsstatus auf bezahlt mit dem Schalter `E-Mail an Kunden senden` ist aktiviert + # 1.2.9 * Das Neuladen der verbleibenden Download-Daten in Shopware >= v6.3.2.0 wurde behoben. Der verbleibende Download kann aktualisiert werden, nachdem Sie auf "Jetzt herunterladen" geklickt haben diff --git a/CHANGELOG_en-GB.md b/CHANGELOG_en-GB.md index a0c8e1b..1d9af61 100644 --- a/CHANGELOG_en-GB.md +++ b/CHANGELOG_en-GB.md @@ -1,3 +1,7 @@ +# 1.2.10 +* Fixed the url in the mail template, change from url() to rawUrl() to get the sales channel domain +* Improved send mail feature, you can send the esd email to the buyer buy change the payment status to paid with the `Send email to customer` toggle is enable + # 1.2.9 * Fixed reload the remaining download data on Shopware >= v6.3.2.0, can update the remaining download after click download now diff --git a/composer.json b/composer.json index b2d5583..39ba5b9 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description":"ESD / Download plugin", "type":"shopware-platform-plugin", "keywords": ["esd", "download"], - "version":"1.2.9", + "version":"1.2.10", "license":"proprietary", "authors":[ { diff --git a/src/Checkout/Cart/Subscriber/OrderPlacedSubscriber.php b/src/Checkout/Cart/Subscriber/OrderPlacedSubscriber.php index becc0f0..82bc17e 100644 --- a/src/Checkout/Cart/Subscriber/OrderPlacedSubscriber.php +++ b/src/Checkout/Cart/Subscriber/OrderPlacedSubscriber.php @@ -60,6 +60,7 @@ public function __invoke(CheckoutOrderPlacedEvent $event): void $products = $this->productRepository->search($criteria, $event->getContext())->getEntities(); if ($products->count() > 0) { $this->esdOrderService->addNewEsdOrders($event->getOrder(), $event->getContext(), $products); + $this->esdOrderService->sendMail($event->getOrder(), $event->getContext()); } } } diff --git a/src/Migration/Migration1606395595FixUrlMailTemplate.php b/src/Migration/Migration1606395595FixUrlMailTemplate.php new file mode 100644 index 0000000..106e99c --- /dev/null +++ b/src/Migration/Migration1606395595FixUrlMailTemplate.php @@ -0,0 +1,72 @@ +executeQuery( + 'SELECT `id` from `mail_template_type` WHERE `technical_name` = :type', + ['type' => EsdMailTemplate::TEMPLATE_TYPE_DOWNLOAD_TECHNICAL_NAME] + )->fetchColumn(); + + $templateId = $connection->executeQuery( + 'SELECT `id` from `mail_template` WHERE `mail_template_type_id` = :typeId', + ['typeId' => $templateTypeId] + )->fetchColumn(); + + if (!empty($templateId)) { + $mailTemplateTranslations = $connection->executeQuery( + 'SELECT `language_id`, `content_html`, `content_plain` from `mail_template_translation` WHERE `mail_template_id` = :templateId', + ['templateId' => $templateId] + )->fetchAll(); + + + foreach ($mailTemplateTranslations as $mailTemplateTranslation) { + $contentHtml = $this->replaceUrlToRawUrl($mailTemplateTranslation['content_html']); + $contentPlain = $this->replaceUrlToRawUrl($mailTemplateTranslation['content_plain']); + $this->updateMailTemplateContent($connection, $contentHtml, $contentPlain, $templateId, $mailTemplateTranslation['language_id']); + } + } + } + + private function updateMailTemplateContent(Connection $connection, string $contentHtml, string $contentPlain, string $templateId, string $languageId): void + { + $sqlString = ' + UPDATE `mail_template_translation` + SET `content_html` = :contentHtml, `content_plain` = :contentPlain + WHERE `mail_template_id`= :templateId AND `language_id` = :langId'; + $connection->executeUpdate($sqlString, [ + 'contentHtml' => $contentHtml, + 'contentPlain' => $contentPlain, + 'templateId' => $templateId, + 'langId' => $languageId, + ]); + } + + private function replaceUrlToRawUrl(string $content): string + { + $urlString = "url('frontend.sas.esd.download.guest', {esdOrderId: esdOrderId})"; + $rawString = "rawUrl('frontend.sas.esd.download.guest', {esdOrderId: esdOrderId}, salesChannel.domains|first.url)"; + if (strpos($content, $urlString) !== false ) { + return str_replace($urlString, $rawString, $content); + } + + return $content; + } + + public function updateDestructive(Connection $connection): void + { + // implement update destructive + } +} diff --git a/src/Resources/views/mail-template/de/download-html-mail-template.html.twig b/src/Resources/views/mail-template/de/download-html-mail-template.html.twig index 95f1e89..de6d2e6 100644 --- a/src/Resources/views/mail-template/de/download-html-mail-template.html.twig +++ b/src/Resources/views/mail-template/de/download-html-mail-template.html.twig @@ -28,7 +28,7 @@ {{ esdFiles[lineItem.productId] }} {% if order.orderCustomer.customer.guest %} - + Download {% else %} diff --git a/src/Resources/views/mail-template/de/download-plain-mail-template.html.twig b/src/Resources/views/mail-template/de/download-plain-mail-template.html.twig index e42ab48..3482041 100644 --- a/src/Resources/views/mail-template/de/download-plain-mail-template.html.twig +++ b/src/Resources/views/mail-template/de/download-plain-mail-template.html.twig @@ -8,7 +8,7 @@ Nachstehend findest du die Details zu deinen digitalen Download mit der Bestelln {% set esdOrderIds = esdOrderListIds[lineItem.id] %} {% for esdOrderId in esdOrderIds %} {% set pos = pos + 1 %} -Pos.: {{ pos }} | Name: {{ lineItem.label|u.wordwrap(80) }} | Dateigröße: {{ esdFiles[lineItem.productId] }} | Download Link: {% if order.orderCustomer.customer.guest %}{{ url('frontend.sas.esd.download.guest', {esdOrderId: esdOrderId}) }}{% else %}{{ url('frontend.sas.esd.download', {esdOrderId: esdOrderId}) }}{% endif %} +Pos.: {{ pos }} | Name: {{ lineItem.label|u.wordwrap(80) }} | Dateigröße: {{ esdFiles[lineItem.productId] }} | Download Link: {% if order.orderCustomer.customer.guest %}{{ rawUrl('frontend.sas.esd.download.guest', {esdOrderId: esdOrderId}, salesChannel.domains|first.url) }}{% else %}{{ url('frontend.sas.esd.download', {esdOrderId: esdOrderId}) }}{% endif %} {% endfor %} {% endif %} diff --git a/src/Resources/views/mail-template/download-html-mail-template.html.twig b/src/Resources/views/mail-template/download-html-mail-template.html.twig index 24988f2..f29d20c 100644 --- a/src/Resources/views/mail-template/download-html-mail-template.html.twig +++ b/src/Resources/views/mail-template/download-html-mail-template.html.twig @@ -28,7 +28,7 @@ {{ esdFiles[lineItem.productId] }} {% if order.orderCustomer.customer.guest %} - + Download {% else %} diff --git a/src/Resources/views/mail-template/download-plain-mail-template.html.twig b/src/Resources/views/mail-template/download-plain-mail-template.html.twig index 3b25b92..db958cd 100644 --- a/src/Resources/views/mail-template/download-plain-mail-template.html.twig +++ b/src/Resources/views/mail-template/download-plain-mail-template.html.twig @@ -8,7 +8,7 @@ Please find the details of your download product of order {{order.orderNumber}}. {% set esdOrderIds = esdOrderListIds[lineItem.id] %} {% for esdOrderId in esdOrderIds %} {% set pos = pos + 1 %} -Pos.: {{ pos }} | Name: {{ lineItem.label|u.wordwrap(80) }} | File size: {{ esdFiles[lineItem.productId] }} | Download link: {% if order.orderCustomer.customer.guest %}{{ url('frontend.sas.esd.download.guest', {esdOrderId: esdOrderId}) }}{% else %}{{ url('frontend.sas.esd.download', {esdOrderId: esdOrderId}) }}{% endif %} +Pos.: {{ pos }} | Name: {{ lineItem.label|u.wordwrap(80) }} | File size: {{ esdFiles[lineItem.productId] }} | Download link: {% if order.orderCustomer.customer.guest %}{{ rawUrl('frontend.sas.esd.download.guest', {esdOrderId: esdOrderId}, salesChannel.domains|first.url) }}{% else %}{{ url('frontend.sas.esd.download', {esdOrderId: esdOrderId}) }}{% endif %} {% endfor %} {% endif %} diff --git a/src/Service/EsdOrderService.php b/src/Service/EsdOrderService.php index a1df95e..270094a 100644 --- a/src/Service/EsdOrderService.php +++ b/src/Service/EsdOrderService.php @@ -2,14 +2,16 @@ namespace Sas\Esd\Service; -use Sas\Esd\Content\Product\Extension\Esd\Aggregate\EsdSerial\EsdSerialEntity; +use Sas\Esd\Content\Product\Extension\Esd\Aggregate\EsdOrder\EsdOrderEntity; use Sas\Esd\Content\Product\Extension\Esd\EsdEntity; +use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity; use Shopware\Core\Checkout\Order\OrderEntity; use Shopware\Core\Content\Product\ProductCollection; use Shopware\Core\Framework\Context; use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult; +use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter; use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; use Shopware\Core\Framework\Uuid\Uuid; @@ -46,9 +48,6 @@ public function addNewEsdOrders( ?ProductCollection $products = null ): void { $newEsdOrders = []; - $esdSerials = []; - $esdOrderListIds = []; - $esdOrderLineItems = []; foreach ($order->getLineItems() as $orderLineItem) { if ($products instanceof ProductCollection) { $product = $products->get($orderLineItem->getProductId()); @@ -76,37 +75,86 @@ public function addNewEsdOrders( $serialId = null; if (!empty($fetchSerialIds)) { $serialId = current($fetchSerialIds); - - /** @var EsdSerialEntity $serial */ - $serial = $fetchSerials->get($serialId); - $esdSerials[] = [ - 'serial' => $serial->getSerial(), - 'productName' => $orderLineItem->getLabel(), - ]; - unset($fetchSerialIds[$serialId]); } - $newEsdOrderId = Uuid::randomHex(); $newEsdOrders[] = [ - 'id' => $newEsdOrderId, + 'id' => Uuid::randomHex(), 'esdId' => $esd->getId(), 'orderLineItemId' => $orderLineItem->getId(), 'serialId' => $serialId, ]; - $esdOrderListIds[$orderLineItem->getId()][] = $newEsdOrderId; - - $esdOrderLineItems[$orderLineItem->getId()] = $orderLineItem; } } if (!empty($newEsdOrders)) { $this->esdOrderRepository->create($newEsdOrders, $context); - $this->esdMailService->sendMailDownload($order, $esdOrderLineItems, $esdOrderListIds, $context); - $this->esdMailService->sendMailSerial($order, $esdSerials, $context); } } + public function sendMail(OrderEntity $order, Context $context): void { + $esdSerials = []; + $esdOrderListIds = []; + $esdOrderLineItems = []; + + $criteria = new Criteria(); + $criteria->addAssociation('orderLineItem'); + $criteria->addAssociation('serial'); + $criteria->addAssociation('esd.esdMedia'); + $criteria->addFilter( + new EqualsAnyFilter('orderLineItemId', array_values($order->getLineItems()->getIds())) + ); + + $esdOrders = $this->esdOrderRepository->search($criteria, $context); + + $esdByLineItemIds = []; + /** @var EsdOrderEntity $esdOrder */ + foreach ($esdOrders->getEntities() as $esdOrder) { + $esd = $esdOrder->getEsd(); + if ($esd === null || $esd->getEsdMedia() === null) { + continue; + } + + $esdOrderLineItems[$esdOrder->getOrderLineItemId()] = $esdOrder->getOrderLineItem(); + $esdByLineItemIds[$esdOrder->getOrderLineItemId()] = $esd; + } + + /** @var OrderLineItemEntity $orderLineItem */ + foreach ($order->getLineItems() as $orderLineItem) { + if (empty($esdByLineItemIds[$orderLineItem->getId()])) { + continue; + } + + $esd = $esdByLineItemIds[$orderLineItem->getId()]; + if ($esd === null || $esd->getEsdMedia() === null) { + continue; + } + + $esdOrder = $esdOrders->filter(function (EsdOrderEntity $esdOrderEntity) use ($orderLineItem) { + return $esdOrderEntity->getOrderLineItemId() === $orderLineItem->getId(); + })->first(); + + for ($q = 0; $q < $orderLineItem->getQuantity(); ++$q) { + $esdOrderListIds[$orderLineItem->getId()][] = $esdOrder->getId(); + } + } + $this->esdMailService->sendMailDownload($order, $esdOrderLineItems, $esdOrderListIds, $context); + + $serialOfEsdOrders = $esdOrders->filter(function (EsdOrderEntity $esdOrderEntity) { + return $esdOrderEntity->getSerialId() !== null; + }); + + /** @var EsdOrderEntity $serialOfEsdOrder */ + foreach ($serialOfEsdOrders as $serialOfEsdOrder) { + $esdSerials[] = [ + 'serial' => $serialOfEsdOrder->getSerial()->getSerial(), + 'productName' => $serialOfEsdOrder->getOrderLineItem()->getLabel(), + ]; + } + + $this->esdMailService->sendMailSerial($order, $esdSerials, $context); + } + public function fetchSerials(EsdEntity $esd, Context $context): ?EntitySearchResult { if (!$esd->hasSerial()) { diff --git a/src/Subscriber/OrderStateChangedSubscriber.php b/src/Subscriber/OrderStateChangedSubscriber.php index 04375a0..cd35c93 100644 --- a/src/Subscriber/OrderStateChangedSubscriber.php +++ b/src/Subscriber/OrderStateChangedSubscriber.php @@ -7,6 +7,8 @@ use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity; use Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent; use Shopware\Core\Checkout\Order\OrderEntity; +use Shopware\Core\Content\MailTemplate\Subscriber\MailSendSubscriber; +use Shopware\Core\Content\MailTemplate\Subscriber\MailSendSubscriberConfig; use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface; use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; @@ -49,6 +51,11 @@ public static function getSubscribedEvents() public function orderStatePaid(OrderStateMachineStateChangeEvent $event): void { + $extension = $event->getContext()->getExtension(MailSendSubscriber::MAIL_CONFIG_EXTENSION); + if (!$extension instanceof MailSendSubscriberConfig) { + $extension = new MailSendSubscriberConfig(false, [], []); + } + $criteria = new Criteria([$event->getOrder()->getId()]); $criteria->addAssociation('lineItems.product.esd.esdMedia'); $criteria->addAssociation('orderCustomer.customer'); @@ -63,16 +70,20 @@ public function orderStatePaid(OrderStateMachineStateChangeEvent $event): void /** @var OrderEntity|null $order */ $order = $this->orderRepository->search($criteria, $event->getContext())->get($event->getOrder()->getId()); if (!empty($order)) { - if (!empty($order->getLineItems())) { + if (!empty($order->getLineItems()) && $order->getAmountTotal() > 0) { $orderLineItemIds = array_filter($order->getLineItems()->fmap(static function (OrderLineItemEntity $orderLineItem) { return $orderLineItem->getId(); })); $esdOrders = $this->esdService->getEsdOrderByOrderLineItemIds($orderLineItemIds, $event->getContext()); - if (empty($esdOrders->first()) && $order->getAmountTotal() > 0) { + if (empty($esdOrders->first())) { $this->esdOrderService->addNewEsdOrders($order, $event->getContext()); } } + + if (!$extension->skip()) { + $this->esdOrderService->sendMail($order, $event->getContext()); + } } } }