[英]How to get all orders of current user in woocommerce
我想在插件函數中獲取當前用戶的所有訂單。
我正在使用這個:
function get_all_orders(){
$customer_orders = get_posts( apply_filters( 'woocommerce_my_account_my_orders_query', array(
'numberposts' => $order_count,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types( 'view-orders' ),
'post_status' => array_keys( wc_get_order_statuses() )
) ) );
return $customer_orders;
}
這在主題中運行良好,但在自定義插件中它不返回任何內容。 難道我做錯了什么? 我應該先打電話給一些 WooCommerce 課程嗎?
自從最初的問題以來,API 可能已經改變,但這更加優雅,並且使用了 WC 自己的函數:
$args = array(
'customer_id' => $user_id,
'limit' => -1, // to retrieve _all_ orders by this user
);
$orders = wc_get_orders($args);
您可以使用更多其他 $args 。
if (!class_exists('WooCommerce')) :
require ABSPATH . 'wp-content/plugins/woocommerce/woocommerce.php';
$orders = get_all_orders();
endif;
function get_all_orders() {
$customer_orders = get_posts(apply_filters('woocommerce_my_account_my_orders_query', array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => get_current_user_id(),
'post_type' => wc_get_order_types('view-orders'),
'post_status' => array_keys(wc_get_order_statuses())
)));
return $customer_orders;
}
試試這個代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.