Created
February 27, 2017 20:29
-
-
Save stephanieland352/182355e34139227bd2762f0281d1afd4 to your computer and use it in GitHub Desktop.
Woocommerce display total sales per product
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function dw_product_totals() { | |
global $wpdb; | |
$post = get_post( $post_id ); | |
$current_product = get_the_ID($post); | |
$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 | |
" )); | |
$current = array($current_product); | |
foreach($order_items as $item) { | |
if(in_array($item->product_id, $current)) { | |
$totalDonations = $item->line_total; | |
} | |
} | |
$totalDonations = number_format($totalDonations, 2, '.', ''); | |
echo 'total donations = '. $totalDonations; | |
} |
'post_id' => $id,
Have to call the post_id as an object. But this code helped me a lot. Thanks to you bro.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Isn't that
COUNT( order_item_meta.meta_value ) as line_total
instead ofSUM( order_item_meta.meta_value ) as line_total
?