'advanced-customer-reports', ); if ( !empty( $customer['is_guest'] ) ) { $args['customer_email'] = $customer['email']; } else { $args['user_id'] = (int) $customer['user_id']; $args['username'] = $customer['username']; } // add_query_arg() does not encode values, and a "+" left in an address // such as "sam+shop@example.org" would arrive back as a space. return add_query_arg( rawurlencode_deep( $args ), admin_url( 'admin.php' ) ); } /** * Register the Customer Summary box, for users who can open reports. * * @return void */ function acreports_add_order_customer_summary_box() { if ( !acreports_current_user_can_view_reports() ) { return; } foreach ( acreports_get_order_screen_ids() as $screen_id ) { add_meta_box( 'acreports-customer-summary', __( 'Customer Summary', 'advanced-customer-reports-woo' ), 'acreports_render_order_customer_summary_box', $screen_id, 'side', 'default' ); } } add_action( 'add_meta_boxes', 'acreports_add_order_customer_summary_box' ); /** * Load the box's styles on the order screens only. * * @return void */ function acreports_enqueue_order_summary_styles() { $screen = ( function_exists( 'get_current_screen' ) ? get_current_screen() : null ); if ( !$screen || !in_array( $screen->id, acreports_get_order_screen_ids(), true ) || !acreports_current_user_can_view_reports() ) { return; } $main_file = dirname( __DIR__ ) . '/advanced-customer-reports.php'; $style_path = dirname( __DIR__ ) . '/css/order-summary.css'; wp_enqueue_style( 'acreports-order-summary', plugins_url( 'css/order-summary.css', $main_file ), array(), ( file_exists( $style_path ) ? (string) filemtime( $style_path ) : '1.0.0' ) ); } add_action( 'admin_enqueue_scripts', 'acreports_enqueue_order_summary_styles' ); /** * What the Customer Summary box shows about an order's customer. * * The figures come from the same report data, over the same orders, as the * customer's unfiltered report, so the box and the "View full report" link * always agree. * * @param WC_Order $order Order being edited. * @return array|null Summary, or null when the order has no customer yet (a new * order with no account or billing email). */ function acreports_get_order_customer_summary( $order ) { $customer = acreports_resolve_customer_context( array( 'user_id' => $order->get_customer_id(), 'customer_email' => $order->get_billing_email(), ) ); if ( empty( $customer['found'] ) ) { return null; } $report_args = array_merge( array( 'date_after' => '', 'date_before' => '', 'orderby' => 'ID', 'order' => 'DESC', 'status' => acreports_get_report_order_statuses( 'all' ), ), acreports_get_customer_order_query_args( $customer ) ); $report_data = acreports_get_report_data( $report_args ); $total = (int) $report_data['total_orders']; return array( 'customer' => $customer, 'report_data' => $report_data, 'total_orders' => $total, 'total_spent' => (float) $report_data['total_spent'], 'average_order_value' => ( $total ? $report_data['total_spent'] / $total : 0 ), 'first_order_date' => $report_data['first_order_date'], 'last_order_date' => $report_data['last_order_date'], 'refunded_orders' => (int) $report_data['refunded_order_count'], 'refund_rate' => acreports_get_percentage( $report_data['refunded_order_count'], $total ), 'report_url' => acreports_get_customer_report_url( $customer ), ); } /** * Render the Customer Summary box. * * @param WP_Post|WC_Order $post_or_order The order on HPOS, its post on the legacy storage. * @return void */ function acreports_render_order_customer_summary_box( $post_or_order ) { $order = ( $post_or_order instanceof WC_Order ? $post_or_order : wc_get_order( ( $post_or_order instanceof WP_Post ? $post_or_order->ID : 0 ) ) ); if ( !$order instanceof WC_Order ) { return; } $summary = acreports_get_order_customer_summary( $order ); if ( !$summary ) { echo '

' . esc_html__( 'Add a customer or billing email to this order to see their history here.', 'advanced-customer-reports-woo' ) . '

'; return; } $customer = $summary['customer']; $date_format = get_option( 'date_format' ); ?>

getTimestamp() ) ); ?>
can_use_premium_code() ) { ?>