求助:在WooCommerce用户账户仪表盘显示订单通知
解决WooCommerce账户仪表盘显示订单通知的问题
错误原因
你遇到的问题很典型——原来的代码是为**单个订单页面(view-order.php)**写的,那里的$order变量是当前正在查看的具体订单对象,所以能调用get_customer_order_notes()获取该订单的通知。但在账户仪表盘(dashboard.php)里,并没有这个预设的$order对象,直接调用方法自然会返回null并报错。
解决方案
我们需要获取当前登录用户的所有订单通知,而不是单个订单的。这里提供两种可靠的实现方式:
方式1:直接查询用户的所有订单通知(高效推荐)
订单通知本质是WordPress的评论(评论类型为order_note,且标记为客户可见),我们可以直接通过WP_Query查询当前用户的相关通知:
defined( 'ABSPATH' ) || exit; // 获取当前登录用户ID $current_user_id = get_current_user_id(); if ( $current_user_id ) { // 查询当前用户的所有客户订单通知 $notes_query = new WP_Query( array( 'comment_type' => 'order_note', 'user_id' => $current_user_id, 'meta_query' => array( array( 'key' => 'is_customer_note', 'value' => '1', 'compare' => '=', ), ), 'orderby' => 'comment_date', 'order' => 'DESC', 'number' => 10, // 限制显示最新的10条,可调整 ) ); $notes = $notes_query->comments; if ( $notes ) : ?> <h2><?php esc_html_e( 'Recent Order Updates', 'woocommerce' ); ?></h2> <ol class="woocommerce-OrderUpdates commentlist notes"> <?php foreach ( $notes as $note ) : // 获取对应的订单对象(可选,用于显示订单号) $order = wc_get_order( $note->comment_post_ID ); ?> <li class="woocommerce-OrderUpdate comment note"> <div class="woocommerce-OrderUpdate-inner comment_container"> <div class="woocommerce-OrderUpdate-text comment-text"> <p class="woocommerce-OrderUpdate-meta meta"> <?php // 显示日期和关联订单号 echo date_i18n( esc_html__( 'l jS \\o\\f F Y, h:ia', 'woocommerce' ), strtotime( $note->comment_date ) ); if ( $order ) { echo ' | ' . esc_html__( 'Order #', 'woocommerce' ) . $order->get_order_number(); } ?> </p> <div class="woocommerce-OrderUpdate-description description"> <?php echo wpautop( wptexturize( $note->comment_content ) ); ?> </div> <div class="clear"></div> </div> <div class="clear"></div> </div> </li> <?php endforeach; ?> </ol> <?php endif; }
方式2:遍历用户的所有订单收集通知
如果你更习惯用WooCommerce的订单对象方法,可以遍历当前用户的所有订单,收集每个订单的客户通知:
defined( 'ABSPATH' ) || exit; // 获取当前用户的所有订单 $customer_orders = wc_get_orders( array( 'customer_id' => get_current_user_id(), 'limit' => -1, // 获取所有订单 'status' => array_keys( wc_get_order_statuses() ), ) ); $all_notes = array(); // 遍历订单收集客户通知 foreach ( $customer_orders as $order ) { $order_notes = $order->get_customer_order_notes(); if ( $order_notes ) { // 给每条通知添加订单信息,方便显示 foreach ( $order_notes as $note ) { $note->order_id = $order->get_id(); $note->order_number = $order->get_order_number(); $all_notes[] = $note; } } } // 按通知日期倒序排列 usort( $all_notes, function( $a, $b ) { return strtotime( $b->comment_date ) - strtotime( $a->comment_date ); } ); // 限制只显示最新的10条 $all_notes = array_slice( $all_notes, 0, 10 ); if ( $all_notes ) : ?> <h2><?php esc_html_e( 'Recent Order Updates', 'woocommerce' ); ?></h2> <ol class="woocommerce-OrderUpdates commentlist notes"> <?php foreach ( $all_notes as $note ) : ?> <li class="woocommerce-OrderUpdate comment note"> <div class="woocommerce-OrderUpdate-inner comment_container"> <div class="woocommerce-OrderUpdate-text comment-text"> <p class="woocommerce-OrderUpdate-meta meta"> <?php echo date_i18n( esc_html__( 'l jS \\o\\f F Y, h:ia', 'woocommerce' ), strtotime( $note->comment_date ) ); echo ' | ' . esc_html__( 'Order #', 'woocommerce' ) . $note->order_number; ?> </p> <div class="woocommerce-OrderUpdate-description description"> <?php echo wpautop( wptexturize( $note->comment_content ) ); ?> </div> <div class="clear"></div> </div> <div class="clear"></div> </div> </li> <?php endforeach; ?> </ol> <?php endif;
使用说明
- 将上述代码替换你之前复制到
dashboard.php的那段代码 - 可以根据需求调整显示的通知数量(修改
number或array_slice的参数) - 两种方式都保留了原有的样式类,所以会自动适配你的主题样式
内容的提问来源于stack exchange,提问作者shibedibedah
相关产品推荐
相关产品推荐

