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

求WooCommerce支付确认页显示订单详情的正确短代码

在WooCommerce自定义感谢页显示订单详情的正确方法

首先得说明,你用的[woocommerce_orders]短代码确实不对——它是用来展示当前登录用户的所有订单列表,不是单个刚完成的订单详情,所以在支付后的感谢页里当然没法正常显示你要的内容。

下面给你两种靠谱的解决方案,看你更适合哪种:

方案一:自定义专属短代码(适合做独立感谢页)

你可以在主题的functions.php文件或者自定义插件里添加以下代码,创建一个专门用于感谢页的订单详情短代码:

function custom_thankyou_order_details($atts) {
    // 从URL获取当前订单ID(WooCommerce默认会在跳转时传递order_id参数)
    $order_id = isset($_GET['order_id']) ? absint($_GET['order_id']) : 0;
    
    if (!$order_id) {
        return '<p>无法找到订单信息,请检查您的订单链接。</p>';
    }
    
    $order = wc_get_order($order_id);
    
    if (!$order || !$order->exists()) {
        return '<p>无效的订单ID。</p>';
    }
    
    // 输出定制化的订单详情
    ob_start();
    ?>
    <h2>感谢您的付款</h2>
    <div class="order-summary">
        <p><strong>订单号:</strong> <?php echo $order->get_order_number(); ?></p>
        <p><strong>下单时间:</strong> <?php echo wc_format_datetime($order->get_date_created()); ?></p>
        
        <h3>您购买的产品:</h3>
        <table class="woocommerce-table order-details-table">
            <thead>
                <tr>
                    <th>产品名称</th>
                    <th>单价</th>
                    <th>数量</th>
                    <th>小计</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($order->get_items() as $item_id => $item) {
                    $product = $item->get_product();
                    ?>
                    <tr>
                        <td>
                            <?php echo $item->get_name(); ?>
                            <?php echo wc_get_formatted_item_data($item); ?>
                        </td>
                        <td><?php echo wc_price($item->get_subtotal() / $item->get_quantity()); ?></td>
                        <td><?php echo $item->get_quantity(); ?></td>
                        <td><?php echo wc_price($item->get_subtotal()); ?></td>
                    </tr>
                <?php } ?>
            </tbody>
            <tfoot>
                <?php foreach ($order->get_order_item_totals() as $total) { ?>
                    <tr>
                        <th colspan="3"><?php echo $total['label']; ?></th>
                        <td><?php echo $total['value']; ?></td>
                    </tr>
                <?php } ?>
            </tfoot>
        </table>
    </div>
    <?php
    return ob_get_clean();
}
add_shortcode('custom_thankyou_order', 'custom_thankyou_order_details');

添加完代码后,你只需要在自定义感谢页里插入[custom_thankyou_order]这个短代码,就能自动显示刚完成的订单详情了。

方案二:修改WooCommerce默认感谢页(更符合官方规范)

其实WooCommerce自带的“订单收到”页面(也就是默认感谢页)已经包含了订单号、产品详情、总价这些信息。如果你只是想定制页面样式或添加额外内容,更推荐这种方式:

  1. 找到WooCommerce插件目录下的templates/checkout/thankyou.php文件;
  2. 把这个文件复制到你的主题目录下的woocommerce/checkout/文件夹里(如果没有就新建对应的文件夹);
  3. 直接编辑复制过来的thankyou.php文件,修改文字、调整布局都可以,比如把默认的标题改成“感谢您的付款”,或者添加自定义内容。

这种方式不需要额外的短代码,而且能完美继承WooCommerce的订单数据传递逻辑,不容易出问题。

最后要注意:不管用哪种方案,都要确保支付完成后跳转的页面能接收到order_id和key这两个URL参数——WooCommerce默认的跳转逻辑会自动带上这些参数,如果你是手动设置跳转页面,记得要保留参数传递,否则短代码或模板没法获取到正确的订单信息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 03:34:04