You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WooCommerce通知邮件中添加[display_ship_est]短代码?

如何将配送预计日期短代码添加到WooCommerce通知邮件中

方法1:通过代码钩子插入(推荐,无模板覆盖风险)

使用WooCommerce提供的邮件钩子,将短代码内容插入到所有订单通知邮件的合适位置。可以把以下代码添加到你的主题functions.php文件,或者使用专门的代码片段插件(如Code Snippets)来管理:

// 向WooCommerce订单通知邮件添加预计配送日期短代码
add_action( 'woocommerce_email_order_details', 'inject_shipment_estimate_to_emails', 20, 4 );
function inject_shipment_estimate_to_emails( $order, $sent_to_admin, $plain_text, $email ) {
    // 先检查短代码是否存在,防止报错
    if ( shortcode_exists( 'display_ship_est' ) ) {
        if ( ! $plain_text ) {
            // HTML邮件格式:添加样式化的区块
            echo '<div style="margin: 1.5em 0; padding: 1em; background-color: #f5f5f5; border-radius: 4px;">';
            echo '<h4 style="margin: 0 0 0.5em 0; font-size: 1.1em;">预计配送日期</h4>';
            echo do_shortcode( '[display_ship_est]' );
            echo '</div>';
        } else {
            // 纯文本邮件格式:保持简洁排版
            echo "\n\n--- 预计配送日期 ---\n";
            echo do_shortcode( '[display_ship_est]' );
            echo "\n\n";
        }
    }
}

代码说明:

  • 钩子woocommerce_email_order_details会在所有订单通知邮件的订单详情区域触发
  • 优先级设为20,确保内容显示在默认订单详情之后
  • 同时适配HTML和纯文本两种邮件格式,保证所有场景下的可读性
  • 先检查短代码是否存在,避免插件停用后出现错误

如果你的短代码需要传入订单ID参数(部分插件需要),可以把do_shortcode部分修改为:

do_shortcode( '[display_ship_est order_id=' . $order->get_id() . ']' );

方法2:修改WooCommerce邮件模板(不推荐,主题更新会覆盖)

如果偏好直接修改邮件模板,步骤如下:

  1. 从WooCommerce插件目录复制文件templates/emails/email-order-details.php
  2. 将复制的文件粘贴到当前主题目录下的woocommerce/emails/文件夹(如果该路径不存在,手动创建)
  3. 在模板文件的合适位置(比如订单表格结束后)添加以下代码:
<?php if ( shortcode_exists( 'display_ship_est' ) ) : ?>
    <div style="margin: 1.5em 0; padding: 1em; background-color: #f5f5f5; border-radius: 4px;">
        <h4 style="margin: 0 0 0.5em 0; font-size: 1.1em;">预计配送日期</h4>
        <?php echo do_shortcode( '[display_ship_est]' ); ?>
    </div>
<?php endif; ?>

注意事项

  • 优先使用方法1,因为修改模板会在主题更新时被覆盖,需要重新同步
  • 添加代码后,务必测试所有相关的订单通知邮件(如客户确认邮件、发货通知、管理员新订单邮件等)
  • 如果短代码显示异常,检查插件文档是否需要额外参数,或确认插件是否正常激活

内容的提问来源于stack exchange,提问作者Ahodhellysar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 20:30:57