Skip to content

Commit

Permalink
Merge branch 'fix-mail-template-url' into 'master'
Browse files Browse the repository at this point in the history
Fixed URL of mail template

See merge request shape-and-shift/plugins/shopware-esd!20
  • Loading branch information
ChristopherDosin committed Nov 30, 2020
2 parents f2f3466 + ea25aa4 commit 561bcd1
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 26 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG_de-DE.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG_en-GB.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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":[
{
Expand Down
1 change: 1 addition & 0 deletions src/Checkout/Cart/Subscriber/OrderPlacedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
72 changes: 72 additions & 0 deletions src/Migration/Migration1606395595FixUrlMailTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php declare(strict_types=1);

namespace Sas\Esd\Migration;

use Doctrine\DBAL\Connection;
use Sas\Esd\Utils\EsdMailTemplate;
use Shopware\Core\Framework\Migration\MigrationStep;

class Migration1606395595FixUrlMailTemplate extends MigrationStep
{
public function getCreationTimestamp(): int
{
return 1606395595;
}

public function update(Connection $connection): void
{
$templateTypeId = $connection->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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<td style="border-bottom:1px solid #cccccc;">{{ esdFiles[lineItem.productId] }}</td>
<td style="border-bottom:1px solid #cccccc;">
{% if order.orderCustomer.customer.guest %}
<a href="{{ url('frontend.sas.esd.download.guest', {esdOrderId: esdOrderId}) }}">
<a href="{{ rawUrl('frontend.sas.esd.download.guest', {esdOrderId: esdOrderId}, salesChannel.domains|first.url) }}">
Download
</a>
{% else %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<td style="border-bottom:1px solid #cccccc;">{{ esdFiles[lineItem.productId] }}</td>
<td style="border-bottom:1px solid #cccccc;">
{% if order.orderCustomer.customer.guest %}
<a href="{{ url('frontend.sas.esd.download.guest', {esdOrderId: esdOrderId}) }}">
<a href="{{ rawUrl('frontend.sas.esd.download.guest', {esdOrderId: esdOrderId}, salesChannel.domains|first.url) }}">
Download
</a>
{% else %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
86 changes: 67 additions & 19 deletions src/Service/EsdOrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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()) {
Expand Down
15 changes: 13 additions & 2 deletions src/Subscriber/OrderStateChangedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand All @@ -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());
}
}
}
}

0 comments on commit 561bcd1

Please sign in to comment.