首页 > 解决方案 > 修改 WooCommerce 电子邮件模板(保留/处理/完成)

问题描述

我正在将 WooCommerce 插件用于电子商务业务,当我创建订单或更改其状态时,总会发送一封电子邮件以通知订单的当前状态。由于某些当前原因,没有关于产品价格的信息,并且 WooCommerce 电子邮件模板在电子邮件订单详细信息中包含价格、小计和总数据。我需要修改这些模板以删除价格、小计和总计数据,并更改地址标题。

我浏览了 woocommerce 插件文件夹以查找电子邮件模板的位置,并在 Google 上搜索并找到了一个有损解决方案,该解决方案在于删除生成订单数据的钩子,在 WC_Emails 类构造函数中找到,然后再次添加它并将其连接到自定义函数生成所需的结构。该解决方案部分适用于我,就好像我在第一次订单处于暂停状态时创建了一个订单并发送了一封电子邮件并根据需要进行查看,但是,当我将订单的状态更改为例如处理中时,或任何后续订单状态现在发送另一封电子邮件,其中包含两个订单详细信息表,一个由我的自定义函数生成,下面是由 WooCommerce 生成的一个,如下图所示。

在此处输入图像描述

// add the action
add_action( 'woocommerce_email_order_details', 'remove_order_details', 10, 4);
add_action( 'woocommerce_email_order_details','action_woocommerce_email_order_details', 10, 4);

// define the woocommerce_email_order_details callback
function action_woocommerce_email_order_details($order, $sent_to_admin, $plain_text, $email)
{
    $text_align = is_rtl() ? 'right' : 'left';
    ?>
    <h2>
        <?php
        if ($sent_to_admin) {
            $before = '<a class="link" href="' . esc_url($order->get_edit_order_url()) . '">';
            $after = '</a>';
        } else {
            $before = '';
            $after = '';
        }
        /* translators: %s: Order ID. */
        echo wp_kses_post($before . sprintf(__('[Order #%s]', 'woocommerce') . $after . ' (<time datetime="%s">%s</time>)', $order->get_order_number(), $order->get_date_created()->format('c'), wc_format_datetime($order->get_date_created())));
        ?>
    </h2>

    <div style="margin-bottom: 40px;">
        <table class="td" cellspacing="0" cellpadding="6"
               style="width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" border="1">
            <thead>
            <tr>
                <th class="td" scope="col"
                    style="text-align:<?php echo esc_attr($text_align); ?>;"><?php esc_html_e('Product', 'woocommerce'); ?></th>
                <th class="td" scope="col"
                    style="text-align:<?php echo esc_attr($text_align); ?>;"><?php esc_html_e('Quantity', 'woocommerce'); ?></th>
            </tr>
            </thead>
            <tbody>
            <?php foreach ($order->get_items() as $item_id => $item) { ?>
                <tr class="<?php echo esc_attr(apply_filters('woocommerce_order_item_class', 'order_item', $item, $order)); ?>">
                    <td class="td"
                        style="text-align:<?php echo esc_attr($text_align); ?>; vertical-align: middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;">
                        <?php

                        // Product name.
                        echo wp_kses_post(apply_filters('woocommerce_order_item_name', $item->get_name(), $item, false));

                        // allow other plugins to add additional product information here.
                        do_action('woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text);

                        // allow other plugins to add additional product information here.
                        do_action('woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text);

                        ?>
                    </td>
                    <td class="td"
                        style="text-align:<?php echo esc_attr($text_align); ?>; vertical-align:middle; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;">
                        <?php echo wp_kses_post(apply_filters('woocommerce_email_order_item_quantity', $item->get_quantity(), $item)); ?>
                    </td>
                </tr>
            <?php } ?>
            </tbody>
            <tfoot>
            <?php
            $meta_data = $order->get_meta('_custom_px_src');
            if ($meta_data) {
                ?>
                <tr>
                    <th class="td" scope="row" colspan="2"
                        style="text-align:<?php echo esc_attr($text_align); ?>;"><?php esc_html_e(is_rtl() ? 'وصفة طبية' : 'Prescription:'); ?></th>
                    <td class="td">
                        <img src="<?php echo $meta_data['value']; ?>" alt="Prescription image" height="42" width="42">
                        <a href="<?php echo $meta_data['value']; ?>" target="_blank"></a>
                    </td>
                </tr>
                <?php
            }
            if ($order->get_customer_note()) {
                ?>
                <tr>
                    <th class="td" scope="row" colspan="2"
                        style="text-align:<?php echo esc_attr($text_align); ?>;"><?php esc_html_e('Note:', 'woocommerce'); ?></th>
                    <td class="td"
                        style="text-align:<?php echo esc_attr($text_align); ?>;"><?php echo wp_kses_post(wptexturize($order->get_customer_note())); ?></td>
                </tr>
                <?php
            }
            ?>
            </tfoot>
        </table>
    </div>
    <?php
}

function remove_order_details()
{
    $mailer = WC()->mailer(); // get the instance of the WC_Emails class
    remove_action('woocommerce_email_order_details', array($mailer, 'order_details'));
}

我期望的是一个仅与我的自定义函数生成的订单详细信息表相关的解决方案

标签: wordpresswoocommercehook-woocommercewoocommerce-theming

解决方案


我发现导致电子邮件模板中的订单详细信息表重复的问题,它正在添加 woocommerce_email_order_details 删除操作,其优先级等于执行操作,如下所示

add_action( 'woocommerce_email_order_details', 'remove_order_details', 10, 4);
add_action( 'woocommerce_email_order_details','action_woocommerce_email_order_details', 10, 4);

但是,我应该做的是优先考虑具有更高优先级的删除操作

add_action( 'woocommerce_email_order_details', 'remove_order_details', 1, 4);
add_action( 'woocommerce_email_order_details','action_woocommerce_email_order_details', 10, 4); 

推荐阅读