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

WooCommerce未下单新客户我的账户页部分模块消失问题求助

问题根因分析
  • 核心故障出在你提供的**最近订单模块(代码3)**的逻辑漏洞上:新注册未下单的用户没有历史订单,调用$customer->get_last_order()返回的是null值,但你的代码没有对无订单场景做完整的兼容处理,直接在判断外调用$last_order->get_status()、$date_created、$last_order->get_items()等属于订单对象的方法/属性,此时会触发PHP致命错误,直接终止页面后续所有代码的执行。
  • 三个消失的内容挂载优先级都晚于最近订单模块:你给最近订单的挂载优先级是2,而最新文章模块优先级是3、最近浏览商品模块优先级是5,WooCommerce默认的「我的账户」导航菜单挂载优先级也高于2,全部排在错误触发点之后,自然不会被渲染输出。
  • 下单后所有内容恢复正常的原因也对应这个逻辑:用户有订单后$last_order是有效的订单对象,不会触发致命错误,后续代码可以正常执行。
修复方案

把最近订单模块的所有和订单相关的输出逻辑,全部包裹在if ( is_a( $last_order, 'WC_Order' ) )判断内部,无订单时可以输出提示内容或者不输出任何内容,修复后参考代码:

add_action( 'woocommerce_account_dashboard', 'recent_order', 2 );
function recent_order(){
    // 仅对登录用户生效
    if ( ! is_user_logged_in() ) return;

    $user_id = get_current_user_id();
    $customer = new WC_Customer( $user_id );
    $last_order = $customer->get_last_order();

    // 有订单才输出内容,无订单直接返回不输出
    if ( ! is_a( $last_order, 'WC_Order' ) ) {
        // 可选择输出无订单提示,不需要就删掉这行直接return
        echo '<p>您还没有任何订单哦</p>';
        return;
    }

    // 以下是有订单时的逻辑,全部挪到判断内部
    $order_id     = $last_order->get_id();
    $order_data   = $last_order->get_data();
    $order_status = $last_order->get_status();
    $date_created  = $last_order->get_date_created();
    $order_total = $last_order->get_total();
?>
<div class="row last-order">
    <div class="col-md-7">
        <div class="col-3">
            <div class="col-5">
                <div class="col-md-4 order-status-box">
                    <h6 class="status">État : <?php echo esc_html( wc_get_order_status_name( $order_status ) ); ?></h6>
                </div>
                <p class="totalcmdd"><?php echo $order_total."€"; ?></p>
                <?php 
                    setlocale(LC_TIME, 'fr_FR');
                    date_default_timezone_set('Europe/Paris');
                    echo utf8_encode(strftime('%d %B %Y', strtotime($date_created)));
                ?>
                </p>
            </div>
            <div style="display: flex;flex-direction: row;justify-content: space-evenly;align-items: center;margin-right: 1.5em;margin-left: 1.5em;padding: 0.3em;background-color: white;text-transform: uppercase;text-decoration: none;font-size: 13px;">
                <?php foreach ($last_order->get_items() as $item) :
                    $product   = $item->get_product(); 
                    $thumbnail = $product->get_image(array(50, 50));
                    $item_name = '';
                    if ($product->get_image_id() > 0) {
                        $item_name = '<div class="item-thumbnail">' . $thumbnail . '</div>';
                    }
                    echo $item_name . $item->get_name();
                endforeach;
                ?>
            </div>
        </div>
    </div>
</div>
<?php
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 08:48:04