Advertisement
wzul

WooCommerce Get Total Sales

Jun 13th, 2023
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.18 KB | None | 0 0
  1. <?php
  2.  
  3. add_action( 'woocommerce_after_register_post_type',  'wan_get_post' );
  4.  
  5. function wan_get_post() {
  6.   $day = date('w');
  7.   $week_start = date('Y-m-d', strtotime('-'.$day.' days'));
  8.   $week_end = date('Y-m-d', strtotime('+'.(6-$day).' days'));
  9.  
  10.   $wtd_orders = wc_get_orders(
  11.     [
  12.       'date_paid' => $week_start . '...' . $week_end,
  13.     ]
  14.   );
  15.  
  16.   $mtd_orders = wc_get_orders(
  17.     [
  18.       'date_paid' => date('Y-m-01') . '...' . date('Y-m-t'),
  19.     ]
  20.   );
  21.  
  22.   $ytd_orders = wc_get_orders(
  23.     [
  24.       'date_paid' => date('Y-01-01') . '...' . date('Y-m-t'),
  25.     ]
  26.   );
  27.  
  28.   $wtd_total = 0;
  29.   $mtd_total = 0;
  30.   $ytd_total = 0;
  31.  
  32.   foreach( $wtd_orders as $order ) {
  33.     $wtd_total+= round( $order->get_total() * 100);
  34.   }
  35.  
  36.   foreach( $mtd_orders as $order ) {
  37.     $mtd_total+= round( $order->get_total() * 100);
  38.   }
  39.   foreach( $ytd_orders as $order ) {
  40.     $ytd_total+= round( $order->get_total() * 100);
  41.   }
  42.  
  43.   echo 'Total Weekly Sales: RM ' . number_format($wtd_total/ 100, 2) . '<br>';
  44.   echo 'Total Monthly Sales: RM ' . number_format($mtd_total/ 100, 2) . '<br>';
  45.   echo 'Total Yearly Sales: RM ' . number_format($ytd_total/ 100, 2) . '<br>';
  46.   exit;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement