verygoodplugins

Untitled

Sep 24th, 2020
969
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function get_churn_by_year( $year = false ) {
  2.  
  3.     if ( ! isset( $_GET['churn'] ) || ! is_admin() ) {
  4.         return;
  5.     }
  6.  
  7.     $year            = 2019;
  8.     $year_to_compare = $year - 1;
  9.  
  10.     for ( $i = 0; $i < 4; $i++ ) {
  11.  
  12.         $args = array(
  13.             'number'     => -1,
  14.             'date_query' => array(
  15.                 array(
  16.                     'year' => $year_to_compare,
  17.                 ),
  18.             ),
  19.         );
  20.  
  21.         $customers = new EDD_Customer_Query( $args );
  22.  
  23.         $total   = count( $customers->items );
  24.         $renewed = 0;
  25.  
  26.         foreach ( $customers->items as $customer ) {
  27.  
  28.             $args = array(
  29.                 'customer'   => $customer->id,
  30.                 'year'       => $year,
  31.                 'meta_key'   => '_edd_sl_is_renewal',
  32.                 'meta_value' => true,
  33.             );
  34.  
  35.             $payments = new EDD_Payments_Query( $args );
  36.  
  37.             $payments = $payments->get_payments();
  38.  
  39.             if ( ! empty( $payments ) ) {
  40.                 $renewed++;
  41.             }
  42.         }
  43.  
  44.         error_log( 'YEAR ' . $year_to_compare . ' vs ' . $year );
  45.         error_log( 'Total customers: ' . $total . '. Renewed in ' . $year . ': ' . $renewed );
  46.         error_log( 'Renewal rate: ' . ( $renewed / $total ) * 100 . '%' );
  47.  
  48.         $year_to_compare--;
  49.  
  50.     }
  51.  
  52. }
  53.  
  54. add_action( 'init', 'get_churn_by_year' );
RAW Paste Data