args['order'] );
} else {
$this->update_interval_boundary_dates( $query_args['after'], $query_args['before'], $query_args['interval'], $data->intervals );
}
$segmenter->add_intervals_segments( $data, $intervals_query, $table_name );
$this->create_interval_subtotals( $data->intervals );
$this->set_cached_data( $cache_key, $data );
}
return $data;
}
/**
* Get unique products based on user time query
*
* @param string $from_clause From clause with date query.
* @param string $where_time_clause Where clause with date query.
* @param string $where_clause Where clause with date query.
* @return integer Unique product count.
*/
public function get_unique_product_count( $from_clause, $where_time_clause, $where_clause ) {
global $wpdb;
$table_name = self::get_db_table_name();
return $wpdb->get_var(
"SELECT
COUNT( DISTINCT {$wpdb->prefix}wc_order_product_lookup.product_id )
FROM
{$wpdb->prefix}wc_order_product_lookup JOIN {$table_name} ON {$wpdb->prefix}wc_order_product_lookup.order_id = {$table_name}.order_id
{$from_clause}
WHERE
1=1
{$where_time_clause}
{$where_clause}"
); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
}
/**
* Get unique coupons based on user time query
*
* @param string $from_clause From clause with date query.
* @param string $where_time_clause Where clause with date query.
* @param string $where_clause Where clause with date query.
* @return integer Unique product count.
*/
public function get_unique_coupon_count( $from_clause, $where_time_clause, $where_clause ) {
global $wpdb;
$table_name = self::get_db_table_name();
return $wpdb->get_var(
"SELECT
COUNT(DISTINCT coupon_id)
FROM
{$wpdb->prefix}wc_order_coupon_lookup JOIN {$table_name} ON {$wpdb->prefix}wc_order_coupon_lookup.order_id = {$table_name}.order_id
{$from_clause}
WHERE
1=1
{$where_time_clause}
{$where_clause}"
); // phpcs:ignore cache ok, DB call ok, unprepared SQL ok.
}
/**
* Add order information to the lookup table when orders are created or modified.
*
* @param int $post_id Post ID.
* @return int|bool Returns -1 if order won't be processed, or a boolean indicating processing success.
*/
public static function sync_order( $post_id ) {
if ( ! OrderUtil::is_order( $post_id, array( 'shop_order', 'shop_order_refund' ) ) ) {
return -1;
}
$order = wc_get_order( $post_id );
if ( ! $order ) {
return -1;
}
return self::update( $order );
}
/**
* Update the database with stats data.
*
* @param WC_Order|WC_Order_Refund $order Order or refund to update row for.
* @return int|bool Returns -1 if order won't be processed, or a boolean indicating processing success.
*/
public static function update( $order ) {
global $wpdb;
$table_name = self::get_db_table_name();
if ( ! $order->get_id() || ! $order->get_date_created() ) {
return -1;
}
/**
* Filters order stats data.
*
* @param array $data Data written to order stats lookup table.
* @param WC_Order $order Order object.
*
* @since 4.0.0
*/
$data = apply_filters(
'woocommerce_analytics_update_order_stats_data',
array(
'order_id' => $order->get_id(),
'parent_id' => $order->get_parent_id(),
'date_created' => $order->get_date_created()->date( 'Y-m-d H:i:s' ),
'date_paid' => $order->get_date_paid() ? $order->get_date_paid()->date( 'Y-m-d H:i:s' ) : null,
'date_completed' => $order->get_date_completed() ? $order->get_date_completed()->date( 'Y-m-d H:i:s' ) : null,
'date_created_gmt' => gmdate( 'Y-m-d H:i:s', $order->get_date_created()->getTimestamp() ),
'num_items_sold' => self::get_num_items_sold( $order ),
'total_sales' => $order->get_total(),
'tax_total' => $order->get_total_tax(),
'shipping_total' => $order->get_shipping_total(),
'net_total' => self::get_net_total( $order ),
'status' => self::normalize_order_status( $order->get_status() ),
'customer_id' => $order->get_report_customer_id(),
'returning_customer' => $order->is_returning_customer(),
),
$order
);
$format = array(
'%d',
'%d',
'%s',
'%s',
'%s',
'%s',
'%d',
'%f',
'%f',
'%f',
'%f',
'%s',
'%d',
'%d',
);
if ( 'shop_order_refund' === $order->get_type() ) {
$parent_order = wc_get_order( $order->get_parent_id() );
if ( $parent_order ) {
$data['parent_id'] = $parent_order->get_id();
$data['status'] = self::normalize_order_status( $parent_order->get_status() );
}
/**
* Set date_completed and date_paid the same as date_created to avoid problems
* when they are being used to sort the data, as refunds don't have them filled
*/
$data['date_completed'] = $data['date_created'];
$data['date_paid'] = $data['date_created'];
}
// Update or add the information to the DB.
$result = $wpdb->replace( $table_name, $data, $format );
/**
* Fires when order's stats reports are updated.
*
* @param int $order_id Order ID.
*
* @since 4.0.0.
*/
do_action( 'woocommerce_analytics_update_order_stats', $order->get_id() );
// Check the rows affected for success. Using REPLACE can affect 2 rows if the row already exists.
return ( 1 === $result || 2 === $result );
}
/**
* Deletes the order stats when an order is deleted.
*
* @param int $post_id Post ID.
*/
public static function delete_order( $post_id ) {
global $wpdb;
$order_id = (int) $post_id;
if ( ! OrderUtil::is_order( $post_id, array( 'shop_order', 'shop_order_refund' ) ) ) {
return;
}
// Retrieve customer details before the order is deleted.
$order = wc_get_order( $order_id );
$customer_id = absint( CustomersDataStore::get_existing_customer_id_from_order( $order ) );
// Delete the order.
$wpdb->delete( self::get_db_table_name(), array( 'order_id' => $order_id ) );
/**
* Fires when orders stats are deleted.
*
* @param int $order_id Order ID.
* @param int $customer_id Customer ID.
*
* @since 4.0.0
*/
do_action( 'woocommerce_analytics_delete_order_stats', $order_id, $customer_id );
ReportsCache::invalidate();
}
/**
* Calculation methods.
*/
/**
* Get number of items sold among all orders.
*
* @param array $order WC_Order object.
* @return int
*/
protected static function get_num_items_sold( $order ) {
$num_items = 0;
$line_items = $order->get_items( 'line_item' );
foreach ( $line_items as $line_item ) {
$num_items += $line_item->get_quantity();
}
return $num_items;
}
/**
* Get the net amount from an order without shipping, tax, or refunds.
*
* @param array $order WC_Order object.
* @return float
*/
protected static function get_net_total( $order ) {
$net_total = floatval( $order->get_total() ) - floatval( $order->get_total_tax() ) - floatval( $order->get_shipping_total() );
return (float) $net_total;
}
/**
* Check to see if an order's customer has made previous orders or not
*
* @param array $order WC_Order object.
* @param int|false $customer_id Customer ID. Optional.
* @return bool
*/
public static function is_returning_customer( $order, $customer_id = null ) {
if ( is_null( $customer_id ) ) {
$customer_id = \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore::get_existing_customer_id_from_order( $order );
}
if ( ! $customer_id ) {
return false;
}
$oldest_orders = \Automattic\WooCommerce\Admin\API\Reports\Customers\DataStore::get_oldest_orders( $customer_id );
if ( empty( $oldest_orders ) ) {
return false;
}
$first_order = $oldest_orders[0];
$second_order = isset( $oldest_orders[1] ) ? $oldest_orders[1] : false;
$excluded_statuses = self::get_excluded_report_order_statuses();
// Order is older than previous first order.
if ( $order->get_date_created() < wc_string_to_datetime( $first_order->date_created ) &&
! in_array( $order->get_status(), $excluded_statuses, true )
) {
self::set_customer_first_order( $customer_id, $order->get_id() );
return false;
}
// The current order is the oldest known order.
$is_first_order = (int) $order->get_id() === (int) $first_order->order_id;
// Order date has changed and next oldest is now the first order.
$date_change = $second_order &&
$order->get_date_created() > wc_string_to_datetime( $first_order->date_created ) &&
wc_string_to_datetime( $second_order->date_created ) < $order->get_date_created();
// Status has changed to an excluded status and next oldest order is now the first order.
$status_change = $second_order &&
in_array( $order->get_status(), $excluded_statuses, true );
if ( $is_first_order && ( $date_change || $status_change ) ) {
self::set_customer_first_order( $customer_id, $second_order->order_id );
return true;
}
return (int) $order->get_id() !== (int) $first_order->order_id;
}
/**
* Set a customer's first order and all others to returning.
*
* @param int $customer_id Customer ID.
* @param int $order_id Order ID.
*/
protected static function set_customer_first_order( $customer_id, $order_id ) {
global $wpdb;
$orders_stats_table = self::get_db_table_name();
$wpdb->query(
$wpdb->prepare(
// phpcs:ignore Generic.Commenting.Todo.TaskFound
// TODO: use the %i placeholder to prepare the table name when available in the the minimum required WordPress version.
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
"UPDATE {$orders_stats_table} SET returning_customer = CASE WHEN order_id = %d THEN false ELSE true END WHERE customer_id = %d",
$order_id,
$customer_id
)
);
}
/**
* Initialize query objects.
*/
protected function initialize_queries() {
$this->clear_all_clauses();
unset( $this->subquery );
$this->total_query = new SqlQuery( $this->context . '_total' );
$this->total_query->add_sql_clause( 'from', self::get_db_table_name() );
$this->interval_query = new SqlQuery( $this->context . '_interval' );
$this->interval_query->add_sql_clause( 'from', self::get_db_table_name() );
$this->interval_query->add_sql_clause( 'group_by', 'time_interval' );
}
}
Fatal error: Uncaught Error: Class "Automattic\WooCommerce\Admin\API\Reports\Orders\Stats\DataStore" not found in /home/valigeria/public_html/wp-content/plugins/woocommerce/src/Internal/Admin/Schedulers/OrdersScheduler.php:46
Stack trace:
#0 /home/valigeria/public_html/wp-content/plugins/woocommerce/src/Admin/ReportsSync.php(24): Automattic\WooCommerce\Internal\Admin\Schedulers\OrdersScheduler::init()
#1 /home/valigeria/public_html/wp-content/plugins/woocommerce/src/Internal/Admin/FeaturePlugin.php(165): Automattic\WooCommerce\Admin\ReportsSync::init()
#2 /home/valigeria/public_html/wp-content/plugins/woocommerce/src/Internal/Admin/FeaturePlugin.php(98): Automattic\WooCommerce\Internal\Admin\FeaturePlugin->includes()
#3 /home/valigeria/public_html/wp-content/plugins/woocommerce/src/Internal/Admin/FeaturePlugin.php(81): Automattic\WooCommerce\Internal\Admin\FeaturePlugin->on_plugins_loaded()
#4 /home/valigeria/public_html/wp-content/plugins/woocommerce/src/Admin/Composer/Package.php(65): Automattic\WooCommerce\Internal\Admin\FeaturePlugin->init()
#5 [internal function]: Automattic\WooCommerce\Admin\Composer\Package::init()
#6 /home/valigeria/public_html/wp-content/plugins/woocommerce/src/Packages.php(128): call_user_func(Array)
#7 /home/valigeria/public_html/wp-content/plugins/woocommerce/src/Packages.php(64): Automattic\WooCommerce\Packages::initialize_packages()
#8 /home/valigeria/public_html/wp-includes/class-wp-hook.php(324): Automattic\WooCommerce\Packages::on_init('')
#9 /home/valigeria/public_html/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array)
#10 /home/valigeria/public_html/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#11 /home/valigeria/public_html/wp-settings.php(550): do_action('plugins_loaded')
#12 /home/valigeria/public_html/wp-config.php(111): require_once('/home/valigeria...')
#13 /home/valigeria/public_html/wp-load.php(50): require_once('/home/valigeria...')
#14 /home/valigeria/public_html/wp-blog-header.php(13): require_once('/home/valigeria...')
#15 /home/valigeria/public_html/index.php(17): require('/home/valigeria...')
#16 {main}
thrown in /home/valigeria/public_html/wp-content/plugins/woocommerce/src/Internal/Admin/Schedulers/OrdersScheduler.php on line 46