如何在WooCommerce我的账户页面新增板块中获取订单ID调取合同
问题解决方案
你可以直接通过当前登录用户ID拉取其名下所有订单对应的合同数据,不需要依赖传入单个订单ID,调整逻辑和代码即可:
核心调整逻辑
- 我的账户的合同板块本来就要展示当前用户所有已签署的合同,不需要单独指定订单ID
- 先通过
get_current_user_id()获取登录用户ID - 查WooCommerce订单表获取该用户所有有效订单的ID集合
- 用订单ID集合匹配你的合同存储表即可拉到全部对应合同
修正后完整代码
<?php class WooContractMyAccount { private $MyAccountContractsEndPoint = 'registered-contracts'; public function __construct() { add_filter('woocommerce_account_menu_items', array($this, 'MyAccountRegistredContracts'), 10, 1); add_action('woocommerce_account_' . $this->MyAccountContractsEndPoint . '_endpoint', array($this, 'MyAccountRegistredContractsEndPoint'), 10, 1); add_action('init', array($this, 'WooContractInit')); } public function MyAccountRegistredContracts($items) { $items = array($this->MyAccountContractsEndPoint => __('Registered Contracts')) + $items; return $items; } public function WooContractInit() { add_rewrite_endpoint($this->MyAccountContractsEndPoint, EP_ROOT | EP_PAGES); } // 原方法写在了类外面,现已挪到类内部 public function MyAccountRegistredContractsEndPoint() { global $wpdb; $table = $wpdb->prefix . 'registered_contracts'; $table_contracts = $wpdb->prefix . 'contracts'; $order_table = $wpdb->prefix . 'posts'; // WooCommerce订单默认存储在wp_posts表,post_type为shop_order // 获取当前登录用户ID $current_user_id = get_current_user_id(); if(empty($current_user_id)) { echo '<p>请先登录后查看合同</p>'; return; } // 先获取当前用户所有有效订单的ID $order_ids = $wpdb->get_col($wpdb->prepare( "SELECT ID FROM $order_table WHERE post_type = 'shop_order' AND post_status IN ('wc-completed', 'wc-processing', 'wc-on-hold') AND post_author = %d", $current_user_id )); if(empty($order_ids)) { echo '<p>'.__('You do not have a registered contract. ').'</p>'; return; } // 生成订单ID占位符,用于SQL查询 $order_id_placeholders = implode(',', array_fill(0, count($order_ids), '%d')); // 关联查询所有对应合同 $contracts = $wpdb->get_results($wpdb->prepare( "SELECT contracts.name,registeredcontracts.date,registeredcontracts.file FROM $table registeredcontracts LEFT JOIN $table_contracts contracts ON contracts.id=registeredcontracts.contracts WHERE ref IN ($order_id_placeholders)", ...$order_ids )); ?> <div class="woocommerce-MyAccount-content"> <?php if (count($contracts) > 0) : ?> <table class="woocommerce-orders-table woocommerce-MyAccount-orders shop_table shop_table_responsive my_account_orders account-orders-table"> <thead> <tr> <th class="woocommerce-orders-table__header"> <?php _e('Contract') ?> </th> <th class="woocommerce-orders-table__header"> <?php _e('Date') ?> </th> <th class="woocommerce-orders-table__header"> <?php _e('File') ?> </th> </tr> </thead> <tbody> <?php foreach ($contracts as $contract) : ?> <tr> <td><?php _e($contract->name); ?></td> <td><?php echo $contract->date; ?></td> <td><a href="<?php echo CONTRACT_URL . '/' . $contract->file; ?>" target="_blank" class="woocommerce-button button"><?php _e('Download Contract'); ?></a></td> </tr> <?php endforeach; ?> </tbody> </table> <?php else : ?> <p><?php _e('You do not have a registered contract. ') ?></p> <?php endif; ?> </div> <?php } } // 初始化类 new WooContractMyAccount();
额外注意事项
- 代码修改后需要到WordPress后台的「设置-固定链接」页面点击保存,刷新重写规则,否则新加入的合同端点可能出现404错误
- 如果你需要筛选更多订单状态,可以自行调整SQL里的
post_status数组 - 如果你的
registered_contracts表已经存储了用户ID字段,也可以直接按用户ID查合同,不需要关联订单表,查询效率会更高
内容的提问来源于stack exchange,提问作者user14907549
相关产品推荐
相关产品推荐

