WooCommerce如何使用短代码仅展示on-hold状态的订单
你编写的代码仅完成了on-hold状态订单的查询逻辑,直接返回订单对象数组无法生成前端可见的订单表格,需要配合WooCommerce自带的订单模板渲染输出。
完整可运行实现代码
/* Shortcode To Display Only On-hold Orders On A Custom Page */ add_shortcode('account_on_hold', 'get_customer_orders_on_hold'); function get_customer_orders_on_hold() { // 未登录用户直接返回空 if ( ! is_user_logged_in() ) return ''; $user = wp_get_current_user(); // 查询on-hold状态订单 $on_hold_orders = wc_get_orders( array( 'limit' => -1, 'customer_id' => $user->ID, 'status' => array('on-hold'), ) ); // 开启输出缓冲捕获模板内容 ob_start(); // 调用WooCommerce自带订单表格模板渲染数据 wc_get_template( 'myaccount/orders.php', array( 'orders' => $on_hold_orders, 'has_orders' => ! empty($on_hold_orders), ) ); // 返回渲染完成的表格HTML return ob_get_clean(); }
使用说明
- 将上述代码添加到当前主题的
functions.php文件末尾,或者自定义功能插件中即可生效 - 在需要展示待付款订单的页面插入短代码
[account_on_hold]即可正常渲染对应状态的订单表格 - 如果需要适配其他订单状态,只需修改
status参数的取值、调整短代码命名即可复用整套逻辑
内容的提问来源于stack exchange,提问作者Omid Toraby
相关产品推荐
相关产品推荐

