
本文档旨在指导开发者如何在 WooCommerce 的“我的账户”页面自定义版块中获取订单 ID,以便访问与特定订单关联的合同信息。我们将参考 WooCommerce 核心代码的实现方式,利用 wc_get_orders() 函数和用户 ID 来检索订单,并最终获取订单 ID。
获取用户订单
首先,我们需要使用 wc_get_orders() 函数来获取当前用户的订单。此函数允许我们根据不同的参数查询订单,包括用户 ID。
$customer_orders = wc_get_orders(
array(
'customer' => get_current_user_id(),
'limit' => -1, // 获取所有订单,或者设置一个合适的数量限制
'orderby' => 'date',
'order' => 'DESC',
)
);这段代码使用 get_current_user_id() 函数获取当前用户的 ID,并将其传递给 wc_get_orders() 函数。limit 参数设置为 -1 表示获取所有订单。orderby 和 order 参数用于指定订单的排序方式。
循环遍历订单并获取订单 ID
获取订单列表后,我们需要循环遍历这些订单,并从中提取订单 ID。
if ( ! empty( $customer_orders ) ) {
foreach ( $customer_orders as $order ) {
$order_id = $order->get_id();
// 在这里可以使用 $order_id 进行后续操作,例如查询合同信息
echo 'Order ID: ' . $order_id . '<br>';
}
} else {
echo 'No orders found for this user.';
}这段代码首先检查订单列表是否为空。如果不为空,则使用 foreach 循环遍历每个订单对象。对于每个订单对象,我们使用 $order->get_id() 方法获取订单 ID,并将其存储在 $order_id 变量中。然后,我们可以在循环内部使用 $order_id 进行后续操作,例如查询与该订单相关的合同信息。
集成到你的自定义版块
现在,我们将上述代码集成到你的自定义版块 MyAccountRegistredContractsEndPoint() 函数中。
public function MyAccountRegistredContractsEndPoint()
{
global $wpdb;
$table = $wpdb->prefix . 'registered_contracts';
$table_contracts = $wpdb->prefix . 'contracts';
$customer_orders = wc_get_orders(
array(
'customer' => get_current_user_id(),
'limit' => -1, // 获取所有订单,或者设置一个合适的数量限制
'orderby' => 'date',
'order' => 'DESC',
)
);
?><div class="woocommerce-MyAccount-content">
<?php if ( ! empty( $customer_orders ) ) : ?>
<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 ( $customer_orders as $order ) :
$order_id = $order->get_id();
// 获取与订单相关的合同信息
$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 = %d", $order_id));
if ( ! empty( $contracts ) ) :
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;
else :
?>
<tr>
<td colspan="3"><?php _e('No contracts found for this order.'); ?></td>
</tr>
<?php
endif;
endforeach;
?>
</tbody>
</table>
<?php else : ?>
<p><?php _e('You do not have a registered contract. ') ?></p>
<?php endif; ?>
</div>
<?php
}在这个修改后的函数中,我们首先获取用户的订单。然后,我们循环遍历每个订单,并使用订单 ID 查询相关的合同信息。最后,我们将合同信息显示在表格中。如果某个订单没有相关的合同,则显示一条消息。
注意事项
- 性能优化: 如果用户订单数量很大,wc_get_orders() 可能会影响性能。可以考虑使用分页或缓存来优化查询。
- 安全: 确保对 $order_id 进行适当的转义和验证,以防止 SQL 注入攻击。
- 错误处理: 添加适当的错误处理机制,以处理查询失败或其他异常情况。
总结
通过使用 wc_get_orders() 函数和用户 ID,我们可以在 WooCommerce 的“我的账户”页面自定义版块中轻松获取订单 ID。然后,我们可以使用订单 ID 查询与特定订单相关的合同信息,并将其显示给用户。请务必注意性能优化、安全性和错误处理,以确保代码的健壮性和安全性。










