客戶在購買產品時必須確認每個訂單的合同。客戶在未確認本協議的情況下不得購買產品。
我在結帳頁面上為此創建了一個復選框。在客戶確認這一點后,他們購買了產品。我將批準的合同保存到資料庫中。我可以用訂單號找到它。到目前為止,沒有問題。
在我的賬戶頁面上,我創建了一個新部分(例如訂單):合約。從這里,每個客戶都可以訪問合同。但是,我無法從我創建的這個部分訪問合同。因為我無法獲取訂單 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
}
uj5u.com熱心網友回復:
對于此類問題,查看 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然后使用a顯示訂單和必要/期望的詳細資訊
<?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 獲取訂單號嗎?” ,是的,這是可能的,而且確實是正確的方法
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/331150.html
標籤:php WordPress的 求购 订单
