[英]Woocommerce - How to show Order details (my-account) on a separate page
[英]How to show order details (my-account) for a specific product (id) on a separate page in Woocommerce?
我想在單獨的頁面上顯示特定產品 ID 的 woocommerce my-account 最近訂單中的訂單表。 更准確地說,如果訂單中有 product_id 為 X 的產品,則顯示當前用戶的所有最近訂單。
我正在嘗試使用這些參考文獻:
Woocommerce - 如何在單獨的頁面上顯示訂單詳細信息(我的帳戶)
從 Woocommerce 中的產品 ID 獲取所有訂單 ID
但我無法將$product_id
與$user_id = get_current_user_id();
以便當前用戶針對特定產品 id 的所有訂單都可以顯示在單獨的頁面上。
編輯:添加代碼。 我試過這段代碼,但它不起作用。
function orders_from_product_id( $product_id ) {
$order_statuses = array('wc-on-hold', 'wc-processing', 'wc-completed');
$customer_user_id = get_current_user_id(); // current user ID here for example
$customer_orders = wc_get_orders( array(
'meta_key' => '_customer_user',
'meta_value' => $customer_user_id,
'post_status' => $order_statuses,
'numberposts' => -1
) );
foreach($customer_orders as $order ){
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
foreach($order->get_items() as $item_id => $item){
$product_id = method_exists( $item, 'get_product_id' ) ? $item->get_product_id() : $item['product_id'];
if ( $product_id == 326 ) {
ob_start();
wc_get_template( 'myaccount/my-orders.php', array(
'current_user' => get_user_by( 'id', $user_id),
'order_count' => $order_count
) );
return ob_get_clean();
} else {
echo '<p> You donot have any orders.</p>';
}
}
}
}
add_shortcode('woocommerce_orders', 'orders_from_product_id');
從此代碼中,您可以獲取用戶購買的產品的當前用戶產品 ID。
// GET CURR USER
$current_user = wp_get_current_user();
if ( 0 == $current_user->ID ) return;
// GET USER ORDERS (COMPLETED + PROCESSING)
$customer_orders = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $current_user->ID,
'post_type' => wc_get_order_types(),
'post_status' => array_keys( wc_get_is_paid_statuses() ),
) );
// LOOP THROUGH ORDERS AND GET PRODUCT IDS
if ( ! $customer_orders ) return;
$product_ids = array();
foreach ( $customer_orders as $customer_order ) {
$order = wc_get_order( $customer_order->ID );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
$product_ids[] = $product_id;
}
}
$product_ids = array_unique( $product_ids );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.