Guest User

WooCommerce - Display Shop Total Sales Revenue

a guest
Jul 26th, 2017
220
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. add_shortcode( 'show_total_sales', 'THEME_SLUG_show_total_sales_shortcode' );
  2.  
  3. function THEME_SLUG_show_total_sales_shortcode() {
  4.  
  5. /*global $wpdb;
  6.  
  7. $order_items = apply_filters( 'woocommerce_reports_sales_overview_order_items', absint( $wpdb->get_var( "
  8. SELECT SUM( order_item_meta.meta_value )
  9. FROM {$wpdb->prefix}woocommerce_order_items as order_items
  10. LEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta as order_item_meta ON order_items.order_item_id = order_item_meta.order_item_id
  11. LEFT JOIN {$wpdb->posts} AS posts ON order_items.order_id = posts.ID
  12. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_ID
  13. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id )
  14. LEFT JOIN {$wpdb->terms} AS term USING( term_id )
  15. WHERE term.slug IN ('" . implode( "','", apply_filters( 'woocommerce_reports_order_statuses', array( 'completed', 'processing', 'on-hold' ) ) ) . "')
  16. AND posts.post_status = 'publish'
  17. AND tax.taxonomy = 'shop_order_status'
  18. AND order_items.order_item_type = 'line_item'
  19. AND order_item_meta.meta_key = '_qty'
  20. " ) ) );
  21.  
  22. */
  23.  
  24.  
  25. global $wpdb;
  26.  
  27. $order_totals = apply_filters( 'woocommerce_reports_sales_overview_order_totals', $wpdb->get_row( "
  28.  
  29. SELECT SUM(meta.meta_value) AS total_sales, COUNT(posts.ID) AS total_orders FROM {$wpdb->posts} AS posts
  30.  
  31. LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
  32.  
  33. WHERE meta.meta_key = '_order_total'
  34.  
  35. AND posts.post_type = 'shop_order'
  36.  
  37. AND posts.post_status IN ( '" . implode( "','", array( 'wc-completed', 'wc-processing', 'wc-on-hold' ) ) . "' )
  38.  
  39. " ) );
  40.  
  41. return absint( $order_totals->total_sales);
  42.  
  43. echo $order_totals;
  44.  
  45. }
RAW Paste Data