我试图为每个用户总结特定项目,但似乎它不识别当前用户并且它总结了所有客户的所有订单。
我该如何解决这个问题?我缺少什么?
这是我正在使用的代码:
$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;
}
}
答案 0 :(得分:1)
更新2 - 正在努力做到这一点:
在post_id上为wp_postmeta表添加LEFT JOIN:
LEFT JOIN {$wpdb->postmeta} AS postmeta ON order_items.order_id = postmeta.post_id
将这2行插入WHERE:
AND postmeta.meta_key = '_customer_user'
AND postmeta.meta_value = '$customer_id'
然后在客户循环中循环,以获得客户的总和
这是代码:
global $wpdb;
// Set here your product ids
$products = array( 1507, 1406, 1506 );
$all_customers = get_users( 'role=customer' );
foreach ( $all_customers as $customer ) {
$customer_id = $customer->ID;
$total_pr = 0;
$query = $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->postmeta} AS postmeta ON order_items.order_id = postmeta.post_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 postmeta.meta_key = '_customer_user'
AND postmeta.meta_value = '$customer_id'
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
");
$results = apply_filters( 'woocommerce_reports_top_earners_order_items', $query );
foreach ( $results as $values ) {
if ( in_array( $values->product_id, $products ) ) {
$total_pr += $values->line_total;
}
}
echo 'customer ID: ' . $customer->ID . ' | Total PR: ' . number_format( $total_pr, 2 ) . '<br>';
}
经过测试并正常工作。它将使用 Customer IDs
和相应的 Total PR
输出列表。
参考文献: