我試圖總結每個用戶的具體項目,但它似乎無法識別當前用戶,它彙總了所有客戶的所有訂單。WooCommerce我的帳戶頁面 - 獲取當前用戶的訂單查詢
我該如何解決這個問題?我錯過了什麼?
下面是我使用的代碼:
$order_items = apply_filters('woocommerce_reports_top_earners_order_items', $wpdb->get_results("
SELECT order_item_meta_2.meta_value as product_id, SUM(order_item_meta.meta_value) as line_total FROM {$wpdb->prefix}woocommerce_order_items as order_items
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta_2 ON order_items.order_item_id = order_item_meta_2.order_item_id
LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
WHERE posts.post_type = 'shop_order'
AND posts.post_status IN ('" . implode("','", array('wc-completed', 'wc-processing', 'wc-on-hold')) . "')
AND order_items.order_item_type = 'line_item'
AND order_item_meta.meta_key = '_line_total'
AND order_item_meta_2.meta_key = '_product_id'
GROUP BY order_item_meta_2.meta_value
"));
$totalPR = 0;
$Products = array(1507, 1406, 1506);
foreach ($order_items as $item) {
if (in_array($item->product_id, $Products)) {
$totalPR = $item->line_total + $totalPR;
echo $totalPR;
}
}
究竟你的意思是用 「不承認說用戶」?你有錯誤嗎?意外的輸出?還有別的嗎?請[編輯]問題以包含儘可能多的信息! – Carpetsmoker