= (int) $block->context['query']['author']; } if ( ! empty( $block->context['query']['search'] ) ) { $query['s'] = $block->context['query']['search']; } } return $query; } e, $data, $format ); /** * Fires when order's stats reports are updated. * * @param int $order_id Order ID. */ 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 ( 'shop_order' !== get_post_type( $order_id ) && 'shop_order_refund' !== get_post_type( $order_id ) ) { 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. */ 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. * @return bool */ public static function is_returning_customer( $order ) { $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( "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' ); } }