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

如何在WooCommerce中根据配送方式自定义订单感谢页?

如何根据配送方式自定义WooCommerce订单感谢页?

如果你想根据客户选择的配送方式(比如你提到的「Delivery on request」)来定制感谢页的内容(比如修改标题),其实用WooCommerce自带的钩子就能轻松实现,不用动主题模板文件,更安全。下面是具体的实现方案:

一、核心思路

我们需要先获取当前订单的配送方式信息,然后通过条件判断,针对特定配送方式修改感谢页的标题或添加自定义内容。主要用到两个关键钩子:

  • woocommerce_endpoint_order-received_title:专门用来修改感谢页的标题
  • woocommerce_thankyou:用来在感谢页添加/修改额外内容

二、具体代码实现

1. 修改感谢页标题(针对「Delivery on request」)

把下面的代码添加到你的主题functions.php文件里,或者自定义插件的代码中:

add_filter( 'woocommerce_endpoint_order-received_title', 'custom_thankyou_title_based_on_shipping', 10, 2 );
function custom_thankyou_title_based_on_shipping( $title, $endpoint ) {
    // 获取当前订单ID
    $order_id = absint( get_query_var( 'order-received' ) );
    
    // 检查订单是否存在
    if ( $order_id && $order = wc_get_order( $order_id ) ) {
        // 获取订单的配送方式显示名称(也可以用slug,更稳定)
        $shipping_method = $order->get_shipping_method();
        
        // 判断是否是「Delivery on request」配送方式
        if ( 'Delivery on request' === $shipping_method ) {
            $title = '感谢下单!我们将尽快联系您确认配送时间';
        }
        // 你可以添加更多配送方式的判断
        // elseif ( '其他配送方式名称' === $shipping_method ) {
        //     $title = '对应的自定义标题';
        // }
    }
    
    return $title;
}

代码说明:

  • 我们通过get_query_var('order-received')拿到当前的订单ID,然后用wc_get_order()获取订单对象
  • $order->get_shipping_method()返回的是你在后台设置的配送方式显示名称,如果想更稳定(不怕名称修改失效),可以改用$order->get_shipping_methods()[0]->get_method_id()获取配送方式的slug
  • 条件匹配后,直接替换标题内容即可

2. 额外添加自定义提示内容(可选)

如果除了标题,还想针对该配送方式显示更多提示信息(比如提醒客户保持电话畅通),可以用woocommerce_thankyou钩子:

add_action( 'woocommerce_thankyou', 'custom_thankyou_content_based_on_shipping', 10, 1 );
function custom_thankyou_content_based_on_shipping( $order_id ) {
    if ( ! $order_id ) return;
    
    $order = wc_get_order( $order_id );
    $shipping_method = $order->get_shipping_method();
    
    if ( 'Delivery on request' === $shipping_method ) {
        echo '<div class="custom-thankyou-note" style="padding: 15px; background: #f8f9fa; margin: 20px 0;">
                <h3>温馨提示</h3>
                <p>您选择了「按需配送」,我们的客服将在1小时内联系您确认具体配送时间,请保持电话畅通哦!</p>
              </div>';
    }
}

三、注意事项

  • 如果你修改了配送方式的显示名称,记得同步修改代码里的匹配文本;用slug的话就不用怕这个问题
  • 代码尽量不要直接放在主题的functions.php里(主题更新会覆盖),建议用子主题或者自定义插件来添加
  • 如果测试时看不到效果,可以清除WooCommerce缓存和浏览器缓存

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:43:50