a %s placeholder for the formatted source to be inserted * via sprintf(). * * @since 8.5.0 * * @param string $label The label for the order origin. * @param string $source_type The source type. * @param string $source The source. * @param string $formatted_source The formatted source. */ $label = (string) apply_filters( 'wc_order_attribution_origin_label', $label, $source_type, $source, $formatted_source ); if ( false === strpos( $label, '%' ) ) { return $formatted_source; } return sprintf( $label, $formatted_source ); } /** * Get the description for the order attribution field. * * @param string $field The field name. * * @return string */ private function get_field_description( string $field ): string { /* translators: %s is the field name */ $description = sprintf( __( 'Order attribution field: %s', 'woocommerce' ), $field ); /** * Filter the description for the order attribution field. * * @since 8.5.0 * * @param string $description The description for the order attribution field. * @param string $field The field name. */ return (string) apply_filters( 'wc_order_attribution_field_description', $description, $field ); } /** * Get the order history for the customer (data matches Customers report). * * @param mixed $customer_identifier The customer ID or billing email. * * @return array Order count, total spend, and average spend per order. */ private function get_customer_history( $customer_identifier ): array { /* * Exclude the statuses that aren't valid for the Customers report. * 'checkout-draft' is the checkout block's draft order status. `any` is added by V2 Orders REST. * @see /Automattic/WooCommerce/Admin/API/Report/DataStore::get_excluded_report_order_statuses() */ $all_order_statuses = ReportsController::get_order_statuses(); $excluded_statuses = array( 'pending', 'failed', 'cancelled', 'auto-draft', 'trash', 'checkout-draft', 'any' ); // Get the valid customer orders. $args = array( 'limit' => - 1, 'return' => 'objects', 'status' => array_diff( $all_order_statuses, $excluded_statuses ), 'type' => 'shop_order', ); // If the customer_identifier is a valid ID, use it. Otherwise, use the billing email. if ( is_numeric( $customer_identifier ) && $customer_identifier > 0 ) { $args['customer_id'] = $customer_identifier; } else { $args['billing_email'] = $customer_identifier; $args['customer_id'] = 0; } $orders = wc_get_orders( $args ); // Populate the order_count and total_spent variables with the valid orders. $order_count = count( $orders ); $total_spent = 0; foreach ( $orders as $order ) { $total_spent += $order->get_total() - $order->get_total_refunded(); } return array( 'order_count' => $order_count, 'total_spent' => $total_spent, 'average_spent' => $order_count ? $total_spent / $order_count : 0, ); } }