id The ID of the subscriber.
*/
$stats = apply_filters('fluent_crm/contact_purchase_stat_' . $commerceProvider, [], $subscriber->id);
if (!$stats) {
return false;
}
$html = '
';
foreach ($stats as $stat) {
$html .= '
' . $stat['title'] . '' . $stat['value'] . '
';
}
$html .= '
';
return [
'title' => __('Customer Summary', 'fluent-crm'),
'content' => $html
];
}
if (defined('WC_PLUGIN_FILE')) {
$summary = $this->getWooCustomerSummary($subscriber);
if ($summary) {
return [
'title' => __('Customer Summary', 'fluent-crm'),
'content' => $summary
];
}
return false;
}
if (Helper::isEdd3()) {
$customer = fluentCrmDb()->table('edd_customers')
->where('email', $subscriber->email);
if ($subscriber->user_id) {
$customer = $customer->orWhere('user_id', $subscriber->user_id);
}
$customer = $customer->first();
if (!$customer) {
return false;
}
$summaryData = [
'order_count' => $customer->purchase_count,
'lifetime_value' => number_format($customer->purchase_value, 2),
'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(),
'first_order_date' => $customer->date_created
];
$html = $this->formatSummaryData($summaryData, true);
return [
'title' => __('Customer Summary', 'fluent-crm'),
'content' => $html
];
}
return false;
}
public function wooOrders($data, $subscriber)
{
if (!defined('WC_PLUGIN_FILE')) {
return $data;
}
$hasRecount = defined('FLUENTCAMPAIGN') && \FluentCampaign\App\Services\Commerce\Commerce::isEnabled('woo');
$app = fluentCrm();
if ($hasRecount && $app->request->get('will_recount') == 'yes') {
(new \FluentCampaign\App\Services\Integrations\WooCommerce\DeepIntegration)->syncCustomerBySubscriber($subscriber);
}
$page = (int)$app->request->get('page', 1);
$per_page = (int)$app->request->get('per_page', 10);
$sort_by = sanitize_sql_orderby($app->request->get('sort_by', 'id'));
$sort_type = sanitize_sql_orderby($app->request->get('sort_type', 'DESC'));
$valid_columns = ['id', 'date_created', 'total_amount'];
$valid_directions = ['ASC', 'DESC'];
if (!in_array($sort_by, $valid_columns)) {
$sort_by = 'id';
}
if (!in_array(strtoupper($sort_type), $valid_directions)) {
$sort_type = 'DESC';
}
$orders = $this->getWooOrders($subscriber, $sort_by, $sort_type);
$totalOrders = count($orders);
$orders = array_slice($orders, ($page - 1) * $per_page, $per_page);
$formattedOrders = [];
foreach ($orders as $order) {
$item_count = $order->get_item_count() - $order->get_item_count_refunded();
$actionsHtml = '';
$date = ''.'#' . $order->get_order_number().''.wc_format_datetime($order->get_date_created()).'';
$status = ''. Helper::getStatusText($order->get_status()) .'';
$formattedOrders[] = [
'date' => wp_kses_post($date),
'status' => wp_kses_post($status),
/* translators: 1: formatted order total (with currency), 2: number of items */
'total' => wp_kses_post(sprintf(_n('%1$s for %2$s item', '%1$s for %2$s items', $item_count, 'fluent-crm'), $order->get_formatted_order_total(), $item_count)),
'action' => $actionsHtml,
];
}
/**
* Determine the WooCommerce purchase history sidebar HTML in FluentCRM.
*
* This filter allows customization of the HTML content displayed in the WooCommerce purchase sidebar.
*
* @since 2.7.0
*
* @param string The current HTML content of the sidebar.
* @param object $subscriber The subscriber object containing subscriber data.
* @param int $page The current page identifier as an integer.
*/
$sidebarHtml = apply_filters('fluent_crm/woo_purchase_sidebar_html', '', $subscriber, $page);
return [
'data' => $formattedOrders,
'sidebar_html' => $sidebarHtml,
'total' => $totalOrders,
'has_recount' => $hasRecount,
'columns_config' => [
'date' => [
'label' => __('Date', 'fluent-crm'),
'sortable' => true,
'key' => 'date_created_gmt'
],
'status' => [
'label' => __('Status', 'fluent-crm'),
],
'total' => [
'label' => __('Total', 'fluent-crm'),
'width' => '160px',
'sortable' => true,
'key' => 'total_amount'
],
'actions' => [
'label' => __('', 'fluent-crm'),
'width' => '50px'
]
]
];
}
public function getWooCustomerSummary($subscriber)
{
$customerQuery = fluentCrmDb()->table('wc_customer_lookup')
->where('email', $subscriber->email);
if ($subscriber->user_id) {
$customerQuery = $customerQuery->orWhere('user_id', $subscriber->user_id);
}
$customer = $customerQuery->first();
if ($customer) {
$statuses = wc_get_is_paid_statuses();
$statuses = array_map(function ($status) {
return 'wc-' . $status;
}, $statuses);
$orderStats = fluentCrmDb()->table('wc_order_stats')
->where('customer_id', $customer->customer_id)
->whereIn('status', $statuses)
->get();
if ($orderStats->isEmpty()) {
return false;
}
$lifetimeValue = 0;
$orderIds = [];
$firstOrderDate = null;
$lastOrderDate = null;
foreach ($orderStats as $order) {
if (!$firstOrderDate) {
$firstOrderDate = $order->date_created;
}
if (!$lastOrderDate) {
$lastOrderDate = $order->date_created;
}
if (strtotime($order->date_created) < strtotime($firstOrderDate)) {
$firstOrderDate = $order->date_created;
}
if (strtotime($order->date_created) > strtotime($lastOrderDate)) {
$lastOrderDate = $order->date_created;
}
$lifetimeValue += $order->total_sales;
$orderIds[] = $order->order_id;
}
$orderIds = array_unique($orderIds);
$orderCount = count($orderIds);
$data_store = \WC_Data_Store::load('report-customers-stats');
$stat = $data_store->get_data();
$avg_value = $orderCount > 0 ? round($lifetimeValue / $orderCount, 2) : 0;
$summaryData = [
'order_count' => $orderCount,
'lifetime_value' => $lifetimeValue,
'avg_value' => $avg_value,
'stat_avg_count' => $stat->avg_orders_count,
'stat_avg_spend' => $stat->avg_total_spend,
'stat_avg_value' => $stat->avg_avg_order_value,
'currency_sign' => get_woocommerce_currency_symbol(),
'last_order_date' => $lastOrderDate,
'first_order_date' => $firstOrderDate,
];
return $this->formatSummaryData($summaryData, true);
}
return false;
}
/**
* Return EDD 3 order history for the subscriber purchase-history panel.
*/
public function eddOrders($data, $subscriber)
{
if (!Helper::isEdd3()) {
return $data;
}
$app = fluentCrm();
$page = (int)$app->request->get('page', 1);
set_query_var('paged', $page);
$hasRecount = defined('FLUENTCAMPAIGN') && \FluentCampaign\App\Services\Commerce\Commerce::isEnabled('edd');
if ($hasRecount && $app->request->get('will_recount') == 'yes') {
(new \FluentCampaign\App\Services\Integrations\Edd\DeepIntegration)->syncCustomerBySubscriber($subscriber);
}
$sort_by = sanitize_sql_orderby($app->request->get('sort_by', 'id'));
$sort_type = sanitize_sql_orderby($app->request->get('sort_type', 'DESC'));
$per_page = (int)$app->request->get('per_page', 10);
$customer = new \EDD_Customer($subscriber->email);
if (!$customer || !$customer->id) {
return $data;
}
$lasOrderData = '';
/*
* EDD 3 stores orders in the edd_orders table. Legacy edd_payment posts
* are intentionally not queried because EDD 2 is no longer supported.
*/
$totalCount = fluentCrmDb()->table('edd_orders')
->where('customer_id', $customer->id)
->count();
if (!$totalCount) {
return $data;
}
$valid_columns = ['id', 'date_created', 'total'];
$valid_directions = ['ASC', 'DESC'];
if (!in_array($sort_by, $valid_columns)) {
$sort_by = 'id';
}
if (!in_array(strtoupper($sort_type), $valid_directions)) {
$sort_type = 'DESC';
}
$orders = fluentCrmDb()->table('edd_orders')
->where('customer_id', $customer->id)
->orderBy($sort_by, $sort_type)
->limit($per_page)
->offset(($page - 1) * $per_page)
->get();
$formattedOrders = [];
foreach ($orders as $order) {
$orderActionHtml = '';
$date = ''.'#' . $order->id .''.date_i18n(get_option('date_format'), strtotime($order->date_created)).'';
$status = ''. Helper::getStatusText($order->status) .'';
$formattedOrders[] = [
'date' => $date,
'status' => $status,
'total' => edd_currency_filter(edd_format_amount($order->total)),
'action' => $orderActionHtml
];
}
if (!$orders->isEmpty()) {
$lasOrderData = date_i18n(get_option('date_format'), strtotime($orders[0]->date_created));
}
/**
* 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' => '