客户在购买产品时必须为每个订单确认合同。未确认本协议,客户不能购买本产品。
我在结帐页面上为此创建了一个复选框。在客户确认后,他们会购买产品。我将批准的合同保存到数据库中。我可以通过订单号找到它。到目前为止,没有问题。
在我的帐户页面,我创建了一个新的部分(如订单):合同。从这里,每个客户都可以访问合同。但是,我不能从我创建的这个部分访问契约。因为我无法获得订单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';
////The problem is here! ($contracts)
$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 must be written_______));
?><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
}
对于这样的问题,看看它是如何在WooCommerce中默认应用的是有用的,并基于
在/includes/wc-template-functions.php的3157 - 3186行@version 2.5.0我们发现如下:
if ( ! function_exists( 'woocommerce_account_orders' ) ) {
/**
* My Account > Orders template.
*
* @param int $current_page Current page number.
*/
function woocommerce_account_orders( $current_page ) {
$current_page = empty( $current_page ) ? 1 : absint( $current_page );
$customer_orders = wc_get_orders(
apply_filters(
'woocommerce_my_account_my_orders_query',
array(
'customer' => get_current_user_id(),
'page' => $current_page,
'paginate' => true,
)
)
);
wc_get_template(
'myaccount/orders.php',
array(
'current_page' => absint( $current_page ),
'customer_orders' => $customer_orders,
'has_orders' => 0 < $customer_orders->total,
)
);
}
}
可以看到,wc_get_orders()和get_current_user_id()用于获取当前客户的订单。
然后结果($customer_orders
)被传递到myaccount/orders.php模板文件。
在模板文件中,然后使用foreach
来显示订单和必要/所需的详细信息
<?php
foreach ( $customer_orders->orders as $customer_order ) {
$order = wc_get_order( $customer_order ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
// Get order ID
$order_id = $order->get_id();
所以回答你的问题"我可以用当前用户id到达订单号吗?">,是的,这是可能的,并且确实是正确的进行方式