ate' => date_i18n(get_option('date_format'), strtotime($order->date_created)), 'status' => $order->status, 'total' => edd_currency_filter(edd_format_amount($order->total)), 'action' => $orderActionHtml ]; } if ($orders) { $lasOrderData = date_i18n(get_option('date_format'), strtotime($orders[0]->date_created)); } } else { $totalCount = count($customer->get_payment_ids()); if (!$totalCount) { return $data; } $payments = edd_get_payments([ 'customer_id' => $customer->id, 'customer' => $customer->id, 'number' => $per_page, 'offset' => ($page - 1) * $per_page ]); $formattedOrders = []; if ($payments) { foreach ($payments as $payment) { if (!$payment instanceof \EDD_Payment) { $payment = new \EDD_Payment($payment->ID); } $orderActionHtml = '' . __('View Order', 'fluent-crm') . ''; $formattedOrders[] = [ 'order' => '#' . $payment->number, 'date' => date_i18n(get_option('date_format'), strtotime($payment->date)), 'status' => $payment->status_nicename, 'total' => edd_currency_filter(edd_format_amount($payment->total)), 'action' => $orderActionHtml ]; } $lasOrderData = date_i18n(get_option('date_format'), strtotime($payments[0]->post_date)); } } /** * Determine the HTML content displayed in the EDD purchase history sidebar for a subscriber in FluentCRM. * * This filter allows customization of the HTML content that appears in the EDD purchase * history sidebar for a given subscriber on a specific page. * * @since 2.7.0 * * @param string $beforeHtml The HTML content to be displayed before the purchase history. * @param object $subscriber The subscriber object. * @param int $page The current page identifier as an integer. */ $beforeHtml = apply_filters('fluent_crm/edd_purchase_sidebar_html', '', $subscriber, $page); // if (!$beforeHtml && $subscriber->user_id && $page == 1 && $formattedOrders) { // $summaryData = [ // 'order_count' => $customer->purchase_count, // 'lifetime_value' => $customer->purchase_value, // 'avg_value' => ($customer->purchase_count) ? round($customer->purchase_value / $customer->purchase_count, 2) : 'n/a', // 'stat_avg_count' => 0, // 'stat_avg_spend' => 0, // 'stat_avg_value' => 0, // 'currency_sign' => edd_currency_symbol(), // 'last_order_date' => $lasOrderData // ]; // $beforeHtml = $this->formatSummaryData($summaryData); // } return [ 'data' => $formattedOrders, 'total' => $totalCount, 'sidebar_html' => $beforeHtml, 'after_html' => '

' . esc_html__('View Customer Profile', 'fluent-crm') . '

', 'has_recount' => $hasRecount, 'columns_config' => [ 'order' => [ 'label' => __('Order', 'fluent-crm'), 'width' => '100px', 'sortable' => true, 'key' => 'id' ], 'date' => [ 'label' => __('Date', 'fluent-crm'), 'sortable' => true, 'key' => 'edd_orders' ], 'status' => [ 'label' => __('Status', 'fluent-crm'), 'width' => '140px', 'sortable' => false ], 'total' => [ 'label' => __('Total', 'fluent-crm'), 'width' => '120px', 'sortable' => true, 'key' => 'total' ], 'action' => [ 'label' => __('Actions', 'fluent-crm'), 'width' => '100px', 'sortable' => false ] ] ]; } public function payformSubmissions($data, $subscriber) { if (!defined('WPPAYFORM_VERSION')) { return $data; } $app = fluentCrm(); $page = intval($app->request->get('page', 1)); $per_page = intval($app->request->get('per_page', 10)); $query = fluentCrmDb()->table('wpf_submissions') ->select([ 'wpf_submissions.id', 'wpf_submissions.form_id', 'wpf_submissions.currency', 'wpf_submissions.payment_status', 'wpf_submissions.payment_total', 'wpf_submissions.payment_method', 'wpf_submissions.created_at', 'posts.post_title' ]) ->join('posts', 'posts.ID', '=', 'wpf_submissions.form_id') ->where(function ($query) use ($subscriber) { $query->where('wpf_submissions.customer_email', '=', $subscriber->email); if ($subscriber->user_id) { $query->orWhere('wpf_submissions.user_id', '=', $subscriber->user_id); } }) ->where('wpf_submissions.payment_total', '>', 0) ->limit($per_page) ->offset($per_page * ($page - 1)) ->orderBy('wpf_submissions.id', 'desc'); $total = $query->count(); $submissions = $query->get(); $formattedSubmissions = []; foreach ($submissions as $submission) { $submissionUrl = admin_url('admin.php?page=wppayform.php#/edit-form/' . $submission->form_id . '/entries/' . $submission->id . '/view'); $actionUrl = 'View Submission'; $formattedSubmissions[] = [ 'id' => '#' . $submission->id, 'Form Title' => $submission->post_title, 'Payment Total' => wpPayFormFormatMoney($submission->payment_total, $subscriber->form_id), 'Payment Status' => $submission->payment_status, 'Payment Method' => $submission->payment_method, 'Submitted At' => $submission->created_at, 'action' => $actionUrl ]; } return [ 'total' => $total, 'data' => $formattedSubmissions ]; } public function formatSummaryData($data, $bodyOnly = false) { $blocks = []; if (!empty($data['first_order_date'])) { $blocks['Customer Since'] = gmdate(get_option('date_format'), strtotime($data['first_order_date'])); } if (!empty($data['last_order_date'])) { $blocks['Last Order'] = gmdate(get_option('date_format'), strtotime($data['last_order_date'])); } $blocks['Order Count (paid)'] = $data['order_count'] . $this->getPercentChangeHtml($data['order_count'], $data['stat_avg_count']); $blocks['Lifetime Value'] = $data['currency_sign'] . $data['lifetime_value']; $blocks['AOV'] = $data['currency_sign'] . $data['avg_value'] . $this->getPercentChangeHtml($data['avg_value'], $data['stat_avg_value']); $html = '

' . esc_html__("Customer Summary", "fluent-crm") . '

'; $body = ''; $body .= ''; if ($bodyOnly) { return $body; } return $body . '
'; } private function getPercentChangeHtml($value, $refValue) { if (!$refValue || !$value) { return ''; } $change = $value - $refValue; $percentChange = absint(ceil($change / $refValue * 100)); if ($change >= 0) { return '' . $percentChange . '%' . ''; } else { return '' . $percentChange . '%' . ''; } } private function getWooOrders($subscriber, $sort_by, $sort_type) { $email = $subscriber->email; $user = get_user_by('email', $email); // check HPOS is enabled or not if (get_option('woocommerce_custom_orders_table_enabled') === 'yes') { // high performance order is enabled if ($user) { $hposOrders = fluentCrmDb()->table('wc_orders') ->where('status', '!=', 'trash') ->select(['id']) ->where(function ($query) use ($user) { $query->where('customer_id', $user->ID) ->orWhere(function ($query) use ($user) { $query->where('billing_email', $user->user_email) ->where('customer_id', 0); }); }) ->orderBy($sort_by, $sort_type) ->get(); } else { $hposOrders = fluentCrmDb()->table('wc_orders') ->select(['id']) ->where('billing_email', $email) ->where('customer_id', 0) ->orderBy($sort_by, $sort_type) ->get(); } if (!$hposOrders) { return []; } $orders = []; foreach ($hposOrders as $hposOrder) { $order = wc_get_order($hposOrder->id); if ($order) { $orders[$hposOrder->id] = $order; } } return array_values($orders); } $orders = []; // Get all orders by user id $storeUseId = $user ? $user->ID : false; if ($storeUseId) { $userOrders = wc_get_orders([ 'customer_id' => $storeUseId, 'limit' => -1, 'orderby' => $sort_by, 'order' => $sort_type, ]); // Sort orders by total amount manually if ($sort_by === 'total_amount') { $this->wooSortOrdersByTotalAmount($userOrders, $sort_type); } foreach ($userOrders as $order) { $orders[$order->get_id()] = $order; } } // get orders by billing email $guestOrders = wc_get_orders([ 'customer' => $email, 'limit' => -1, 'orderby' => $sort_by, 'order' => $sort_type, ]); if ($sort_by === 'total_amount') { $this->wooSortOrdersByTotalAmount($userOrders, $sort_type); } foreach ($guestOrders as $order) { $userId = $order->get_user_id(); if ($userId && $storeUseId != $userId) { continue; } $orders[$order->get_id()] = $order; } return array_values($orders); } /** * Sorts an array of WooCommerce orders by their total amount. * * This method sorts an array of WooCommerce order objects based on the total order amount. * The sorting can be done in either ascending ('ASC') or descending ('DESC') order, * as specified by the $sort_type parameter. * * @param array $orders Array of WooCommerce order objects to be sorted. * @param string $sort_type Specifies the sorting order. * Accepts 'ASC' for ascending or 'DESC' for descending. * * @return void The $orders array is sorted in place. */ public function wooSortOrdersByTotalAmount(&$orders, $sort_type) { usort($orders, function ($a, $b) use ($sort_type) { $a_total = (float)$a->get_total(); $b_total = (float)$b->get_total(); return $sort_type === 'ASC' ? $a_total <=> $b_total : $b_total <=> $a_total; }); } }