Add gross revenue to Tracks base data.

This commit is contained in:
Jeff Stieler 2019-02-21 11:46:58 -07:00 committed by Paul Sealock
parent 46411b12f5
commit fe841ecc0b
3 changed files with 55 additions and 5 deletions

View File

@ -493,7 +493,7 @@ class WC_Tracker {
*
* @return array
*/
private static function get_order_totals() {
public static function get_order_totals() {
global $wpdb;
$gross_total = $wpdb->get_var( "

View File

@ -49,6 +49,8 @@ class WC_Site_Tracking {
var eventName = '" . WC_Tracks::PREFIX . "' + name;
var eventProperties = properties || {};
eventProperties.url = '" . home_url() . "'
eventProperties.products_count = '" . WC_Tracks::get_products_count() . "'
eventProperties.orders_gross = '" . WC_Tracks::get_total_revenue() . "'
window._tkq = window._tkq || [];
window._tkq.push( [ 'recordEvent', eventName, eventProperties ] );
}

View File

@ -15,6 +15,55 @@ class WC_Tracks {
*/
const PREFIX = 'wcadmin_';
/**
* Option name for total store revenue.
*/
const REVENUE_CACHE_OPTION = 'woocommerce_tracker_orders_totals';
/**
* Initialize necessary hooks.
*/
public static function init() {
add_action( 'woocommerce_new_order', array( __CLASS__, 'update_revenue_cache' ) );
add_action( 'woocommerce_update_order', array( __CLASS__, 'update_revenue_cache' ) );
add_action( 'woocommerce_delete_order', array( __CLASS__, 'update_revenue_cache' ) );
add_action( 'woocommerce_order_refunded', array( __CLASS__, 'update_revenue_cache' ) );
}
/**
* Recalculate store gross revenue and update cache.
*/
public static function update_revenue_cache() {
update_option( self::REVENUE_CACHE_OPTION, WC_Tracker::get_order_totals() );
}
/**
* Get the (cached) store gross revenue total.
*
* @return null|string Store gross revenue, or null if cache error.
*/
public static function get_total_revenue() {
$total_revenue = get_option( self::REVENUE_CACHE_OPTION, false );
if ( false === $total_revenue ) {
$total_revenue = WC_Tracker::get_order_totals();
update_option( self::REVENUE_CACHE_OPTION, $total_revenue );
}
return empty( $total_revenue['gross'] ) ? null : $total_revenue['gross'];
}
/**
* Get total product counts.
*
* @return array
*/
public static function get_products_count() {
$product_counts = WC_Tracker::get_product_counts();
return $product_counts['total'];
}
/**
* Gather blog related properties.
*
@ -22,14 +71,12 @@ class WC_Tracks {
* @return array Blog details.
*/
public static function get_blog_details( $user_id ) {
$product_counts = WC_Tracker::get_product_counts();
return array(
// @todo Add revenue info and url similar to wc-tracker.
'url' => home_url(),
'blog_lang' => get_user_locale( $user_id ),
'blog_id' => ( class_exists( 'Jetpack' ) && Jetpack_Options::get_option( 'id' ) ) || null,
'products_count' => $product_counts['total'],
'products_count' => self::get_products_count(),
'orders_gross' => self::get_total_revenue(),
);
}
@ -98,3 +145,4 @@ class WC_Tracks {
}
}
WC_Tracks::init();