如何在WooCommerce账户页外展示客户所有订单(Elementor自定义仪表盘)
解决Elementor自定义WooCommerce客户订单仪表盘的方法
方法1:自定义专属短代码(推荐)
直接写个短代码,只输出当前用户的订单列表,完全绕过默认账户页的冗余内容。把下面的代码加到主题的functions.php文件或者自定义插件里:
function custom_user_orders_shortcode() { // 只允许登录用户访问 if ( ! is_user_logged_in() ) { return '<p>请先登录查看您的订单</p>'; } $current_user = wp_get_current_user(); $user_id = $current_user->ID; // 查询当前用户的订单(仅显示已完成/处理中的订单,可按需调整) $orders = wc_get_orders( array( 'customer_id' => $user_id, 'status' => array( 'wc-completed', 'wc-processing' ), 'limit' => -1, // 显示所有订单 'orderby' => 'date', 'order' => 'DESC' ) ); if ( empty( $orders ) ) { return '<p>暂无订单记录</p>'; } // 构建订单列表HTML $output = '<div class="custom-orders-list">'; $output .= '<h3>我的订单</h3>'; $output .= '<table class="woocommerce-orders-table woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table">'; $output .= '<thead><tr><th class="woocommerce-orders-table__header woocommerce-orders-table__header-order-number">订单号</th><th class="woocommerce-orders-table__header woocommerce-orders-table__header-order-date">日期</th><th class="woocommerce-orders-table__header woocommerce-orders-table__header-order-status">状态</th><th class="woocommerce-orders-table__header woocommerce-orders-table__header-order-total">总额</th><th class="woocommerce-orders-table__header woocommerce-orders-table__header-order-actions">操作</th></tr></thead>'; $output .= '<tbody>'; foreach ( $orders as $order ) { $order_id = $order->get_id(); $order_date = wc_format_datetime( $order->get_date_created() ); $order_status = wc_get_order_status_name( $order->get_status() ); $order_total = $order->get_formatted_order_total(); $order_actions = wc_get_account_orders_actions( $order ); $output .= '<tr class="woocommerce-orders-table__row woocommerce-orders-table__row--status-' . esc_attr( $order->get_status() ) . ' order">'; $output .= '<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-order-number" data-title="订单号"><a href="' . esc_url( $order->get_view_order_url() ) . '"># ' . $order_id . '</a></td>'; $output .= '<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-order-date" data-title="日期">' . $order_date . '</td>'; $output .= '<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-order-status" data-title="状态"><span class="woocommerce-orders-table__status ' . esc_attr( $order->get_status() ) . '">' . $order_status . '</span></td>'; $output .= '<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-order-total" data-title="总额">' . $order_total . '</td>'; $output .= '<td class="woocommerce-orders-table__cell woocommerce-orders-table__cell-order-actions" data-title="操作">'; if ( ! empty( $order_actions ) ) { foreach ( $order_actions as $key => $action ) { $output .= '<a href="' . esc_url( $action['url'] ) . '" class="woocommerce-button button ' . sanitize_html_class( $key ) . '">' . esc_html( $action['name'] ) . '</a>'; } } $output .= '</td></tr>'; } $output .= '</tbody></table></div>'; return $output; } add_shortcode( 'custom_my_orders', 'custom_user_orders_shortcode' );
写完代码后,在Elementor的自定义仪表盘页面,直接添加「短代码」组件,输入[custom_my_orders]就能只显示干净的订单列表了。
方法2:用Elementor Pro自定义查询(适合可视化操作)
如果用的是Elementor Pro,不用写代码也能实现:
- 新建一个「查询循环」组件
- 在查询设置里,选择「WooCommerce订单」作为查询类型
- 添加筛选条件:「客户」=「当前登录用户」,按需设置订单状态筛选
- 用Elementor动态标签绑定订单字段,比如订单号(动态标签→WooCommerce→订单ID)、订单日期、状态、总额等,自己排版成需要的样式
方法3:过滤默认账户页,隐藏冗余内容
如果想保留默认订单模板但去掉其他标签,可通过以下方式:
方式A:CSS隐藏
在Elementor的页面自定义CSS里添加:
/* 隐藏账户页除订单外的所有标签和内容 */ .woocommerce-MyAccount-navigation ul li:not(.woocommerce-MyAccount-navigation-link--orders) { display: none; } .woocommerce-MyAccount-content > div:not(.woocommerce-MyAccount-orders) { display: none; }
然后在页面里添加短代码[woocommerce_my_account],就只会显示订单标签和对应的内容。
方式B:用钩子移除多余标签
把下面代码加到functions.php:
function remove_my_account_tabs( $tabs ) { // 移除不需要的标签,保留orders unset( $tabs['dashboard'] ); unset( $tabs['edit-address'] ); unset( $tabs['payment-methods'] ); unset( $tabs['edit-account'] ); return $tabs; } add_filter( 'woocommerce_account_menu_items', 'remove_my_account_tabs' );
这样默认的[woocommerce_my_account]就只会显示订单标签和内容了。
内容的提问来源于stack exchange,提问作者Ebubechukwu Ibeh
相关产品推荐
相关产品推荐

