Magento 2:根据电子邮件模板中的送货方式更改电子邮件内容

问题描述

我需要根据运送方式更改电子邮件内容。运送方式有两种,一种是在商店提货,另一种是运送到用户所在地。

对于这两种情况,电子邮件模板的内容如下:

Thank you for your order from XYZ. Once your package ships we will send you a tracking number. You can check the status of your order by logging into your account.

我不要此行“一旦您的包裹寄出,我们将向您发送一个跟踪号。”用于店内取货,并使用其他一些信息更改此行。

我的电子邮件模板示例,例如:

{{template config_path="design/email/header_template"}}

<table>
    <tr class="email-intro">
        <td>
            <p class="greeting">{{trans "%customer_name," customer_name=$order_data.customer_name}}</p>
            <p>
                {{trans "Thank you for your order from %store_name." store_name=$store.frontend_name}}
{{trans "Once your package ships we will send you a tracking number."}}
                {{trans 'You can check the status of your order by <a href="%account_url">logging into your account</a>.' account_url=$this.getUrl($store,'customer/account/',[_nosid:1]) |raw}}
            </p>
            <p>
                {{trans 'If you have questions about your order,you can email us at <a href="mailto:%store_email">%store_email</a>' store_email=$store_email |raw}}{{depend store_phone}} {{trans 'or call us at <a href="tel:%store_phone">%store_phone</a>' store_phone=$store_phone |raw}}{{/depend}}.
                {{depend store_hours}}
                    {{trans 'Our hours are <span class="no-link">%store_hours</span>.' store_hours=$store_hours |raw}}
                {{/depend}}
            </p>
        </td>
    </tr>
    <tr class="email-summary">
        <td>
            <h1>{{trans 'Your Order <span class="no-link">#%increment_id</span>' increment_id=$order.increment_id |raw}}</h1>
            <p>{{trans 'Placed on <span class="no-link">%created_at</span>' created_at=$created_at_formatted |raw}}</p>
        </td>
    </tr>
    <tr class="email-information">
        <td>
            {{depend order_data.email_customer_note}}
            <table class="message-info">
                <tr>
                    <td>
                        {{var order_data.email_customer_note|escape|nl2br}}
                    </td>
                </tr>
            </table>
            {{/depend}}
            <table class="order-details">
                <tr>
                    <td class="address-details">
                        <h3>{{trans "Billing Info"}}</h3>
                        <p>{{var formattedBillingAddress|raw}}</p>
                    </td>
                    {{depend order_data.is_not_virtual}}
                    <td class="address-details">
                        <h3>{{trans "Shipping Info"}}</h3>
                        <p>{{var formattedShippingAddress|raw}}</p>
                    </td>
                    {{/depend}}
                </tr>
                <tr>
                    <td class="method-info">
                        <h3>{{trans "Payment Method"}}</h3>
                        {{var payment_html|raw}}
                    </td>
                    {{depend order_data.is_not_virtual}}
                    <td class="method-info">
                        <h3>{{trans "Shipping Method"}}</h3>
                        <p>{{var order.shipping_description}}</p>
                        {{if shipping_msg}}
                        <p>{{var shipping_msg}}</p>
                        {{/if}}
                    </td>
                    {{/depend}}
                </tr>
            </table>
            {{layout handle="sales_email_order_items" order_id=$order_id area="frontend"}}
        </td>
    </tr>
</table>

{{template config_path="design/email/footer_template"}}

我看到店内取货

{{var order.shipping_description}}

的电子邮件类似于“店内取货- 第二个值就像“固定费率”。

请帮助我更改内容需要设置的条件。

谢谢

解决方法

您可以通过向观察者添加逻辑来实现。

创建基本模块

registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,'Vendor_Module',__DIR__
);

module.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
         <module name="Vendor_Module" setup_version="1.0.0"/>
    </config>

注册观察者,以了解何时加载电子邮件模板

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">    
                <event name="email_order_set_template_vars_before">
            <observer name="name_of_observer" instance="Vendor\Module\Observer\AddVariable" />
        </event>
    </config>

在您的Observer应用程序\代码\供应商\模块\ Observer \ AddVariable.php中

<?php
    namespace Vendor\Module\Observer;
    use Magento\Sales\Model\Order;
    use Magento\Framework\Event\ObserverInterface;
    class AddVariable implements ObserverInterface
    {
        protected $order;
        public function __construct(
            Order $order
        ) {
            $this->order= $order;
        }
        public function execute(\Magento\Framework\Event\Observer $observer)
        {
            /** @var \Magento\Framework\App\Action\Action $controller */
            $transport = $observer->getEvent()->getTransport();
            if($transport->getOrder() != null)
            {
                $order = $this->order->load($transport->getOrder()->getId());
                $shipping_method = $order->getShippingAddress()->getShippingMethod();
                $transport['variable'] = /*some logic ... */
                
            }
        }
    }

$transport['variable']将变量添加到您的数据中。您可以像模板中的其他任何变量一样调用它,并根据需要分配任意数量。