
7折
减价出售
¥799
<tr> ...">
开源CRM解决方案集成
利用WordPress内置用户系统构建CRM基础:
// 扩展用户元数据用于CRM
function add_crm_user_fields($user) {
?>
<h3>CRM信息</h3>
<table class="form-table">
<tr>
<th><label for="company">公司</label></th>
<td>
<input type="text" name="company" id="company"
value="<?php echo esc_attr(get_user_meta($user->ID, 'company', true)); ?>"
class="regular-text">
</td>
</tr>
<tr>
<th><label for="phone">电话</label></th>
<td>
<input type="tel" name="phone" id="phone"
value="<?php echo esc_attr(get_user_meta($user->ID, 'phone', true)); ?>"
class="regular-text">
</td>
</tr>
<tr>
<th><label for="customer_type">客户类型</label></th>
<td>
<select name="customer_type" id="customer_type">
<?php $types = ['lead' => '潜在客户', 'customer' => '正式客户', 'vip' => 'VIP客户']; ?>
<?php foreach ($types as $value => $label): ?>
<option value="<?php echo $value; ?>"
<?php selected(get_user_meta($user->ID, 'customer_type', true), $value); ?>>
<?php echo $label; ?>
</option>
<?php endforeach; ?>
</select>
</td>
</tr>
</table>
<?php
}
add_action('show_user_profile', 'add_crm_user_fields');
add_action('edit_user_profile', 'add_crm_user_fields');
function save_crm_user_fields($user_id) {
if (!current_user_can('edit_user', $user_id)) return false;
update_user_meta($user_id, 'company', sanitize_text_field($_POST['company']));
update_user_meta($user_id, 'phone', sanitize_text_field($_POST['phone']));
update_user_meta($user_id, 'customer_type', sanitize_text_field($_POST['customer_type']));
}
add_action('personal_options_update', 'save_crm_user_fields');
add_action('edit_user_profile_update', 'save_crm_user_fields');
创建客户自定义帖子类型:
function register_customer_post_type() {
$labels = [
'name' => '客户',
'singular_name' => '客户',
'menu_name' => '客户管理',
'add_new' => '添加客户',
'add_new_item' => '添加新客户',
'edit_item' => '编辑客户信息',
'view_item' => '查看客户'
];
$args = [
'labels' => $labels,
'public' => true,
'show_in_menu' => true,
'menu_position' => 25,
'menu_icon' => 'dashicons-businessperson',
'supports' => ['title', 'editor', 'custom-fields'],
'capability_type' => 'post',
'capabilities' => [
'create_posts' => 'create_customers',
'edit_posts' => 'edit_customers',
'edit_others_posts' => 'edit_others_customers'
],
'map_meta_cap' => true
];
register_post_type('customer', $args);
}
add_action('init', 'register_customer_post_type');
// 添加客户元数据框
function add_customer_meta_boxes() {
add_meta_box(
'customer-details',
'客户详细信息',
'render_customer_details',
'customer',
'normal',
'high'
);
}
add_action('add_meta_boxes', 'add_customer_meta_boxes');
function render_customer_details($post) {
$email = get_post_meta($post->ID, '_customer_email', true);
$phone = get_post_meta($post->ID, '_customer_phone', true);
$status = get_post_meta($post->ID, '_customer_status', true);
wp_nonce_field('customer_details_nonce', 'customer_details_nonce');
?>
<div class="customer-fields">
<p>
<label for="customer_email">邮箱:</label>
<input type="email" id="customer_email" name="customer_email"
value="<?php echo esc_attr($email); ?>" class="widefat">
</p>
<p>
<label for="customer_phone">电话:</label>
<input type="tel" id="customer_phone" name="customer_phone"
value="<?php echo esc_attr($phone); ?>" class="widefat">
</p>
<p>
<label for="customer_status">状态:</label>
<select id="customer_status" name="customer_status" class="widefat">
<option value="lead" <?php selected($status, 'lead'); ?>>潜在客户</option>
<option value="active" <?php selected($status, 'active'); ?>>活跃客户</option>
<option value="inactive" <?php selected($status, 'inactive'); ?>>非活跃客户</option>
</select>
</p>
</div>
<?php
}
function save_customer_details($post_id) {
if (!isset($_POST['customer_details_nonce']) ||
!wp_verify_nonce($_POST['customer_details_nonce'], 'customer_details_nonce')) {
return;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
update_post_meta($post_id, '_customer_email', sanitize_email($_POST['customer_email']));
update_post_meta($post_id, '_customer_phone', sanitize_text_field($_POST['customer_phone']));
update_post_meta($post_id, '_customer_status', sanitize_text_field($_POST['customer_status']));
}
add_action('save_post_customer', 'save_customer_details');
class HubSpot_CRM_Integration {
private $api_key;
public function __construct() {
$this->api_key = get_option('hubspot_api_key');
add_action('user_register', [$this, 'sync_user_to_hubspot']);
add_action('profile_update', [$this, 'update_hubspot_contact']);
add_action('wpcf7_mail_sent', [$this, 'handle_form_submission']);
}
public function sync_user_to_hubspot($user_id) {
$user = get_userdata($user_id);
$contact_data = [
'properties' => [
[
'property' => 'email',
'value' => $user->user_email
],
[
'property' => 'firstname',
'value' => $user->first_name
],
[
'property' => 'lastname',
'value' => $user->last_name
]
]
];
$response = wp_remote_post('https://api.hubapi.com/contacts/v1/contact/', [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->api_key
],
'body' => json_encode($contact_data),
'timeout' => 30
]);
}
public function handle_form_submission($contact_form) {
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$data = $submission->get_posted_data();
$this->create_hubspot_contact($data);
}
}
private function create_hubspot_contact($data) {
// HubSpot联系人创建逻辑
}
}
new HubSpot_CRM_Integration();
class Salesforce_Integration {
private $access_token;
public function __construct() {
add_action('init', [$this, 'oauth_authentication']);
add_action('woocommerce_new_order', [$this, 'create_salesforce_lead']);
}
public function oauth_authentication() {
if (isset($_GET['code']) && isset($_GET['state'])) {
$token_response = wp_remote_post('https://login.salesforce.com/services/oauth2/token', [
'body' => [
'grant_type' => 'authorization_code',
'client_id' => get_option('salesforce_client_id'),
'client_secret' => get_option('salesforce_client_secret'),
'redirect_uri' => admin_url('admin.php?page=salesforce-auth'),
'code' => $_GET['code']
]
]);
if (!is_wp_error($token_response)) {
$tokens = json_decode($token_response['body'], true);
update_option('salesforce_access_token', $tokens['access_token']);
update_option('salesforce_refresh_token', $tokens['refresh_token']);
}
}
}
public function create_salesforce_lead($order_id) {
$order = wc_get_order($order_id);
$lead_data = [
'LastName' => $order->get_billing_last_name(),
'FirstName' => $order->get_billing_first_name(),
'Email' => $order->get_billing_email(),
'Company' => $order->get_billing_company(),
'Phone' => $order->get_billing_phone()
];
$response = wp_remote_post('https://your_instance.salesforce.com/services/data/v52.0/sobjects/Lead/', [
'headers' => [
'Authorization' => 'Bearer ' . get_option('salesforce_access_token'),
'Content-Type' => 'application/json'
],
'body' => json_encode($lead_data)
]);
}
}
new Salesforce_Integration();
class CRM_Data_Model {
public function create_tables() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE {$wpdb->prefix}crm_interactions (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
customer_id BIGINT UNSIGNED NOT NULL,
interaction_type VARCHAR(50) NOT NULL,
interaction_date DATETIME NOT NULL,
subject VARCHAR(200) NOT NULL,
notes TEXT,
assigned_to BIGINT UNSIGNED,
status VARCHAR(50) DEFAULT 'pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY customer_id (customer_id),
KEY interaction_type (interaction_type)
) {$charset_collate};";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
public function log_interaction($customer_id, $data) {
global $wpdb;
$wpdb->insert("{$wpdb->prefix}crm_interactions", [
'customer_id' => $customer_id,
'interaction_type' => $data['type'],
'interaction_date' => current_time('mysql'),
'subject' => $data['subject'],
'notes' => $data['notes'],
'assigned_to' => get_current_user_id(),
'status' => $data['status'] ?? 'completed'
]);
}
public function get_customer_interactions($customer_id) {
global $wpdb;
return $wpdb->get_results($wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}crm_interactions
WHERE customer_id = %d
ORDER BY interaction_date DESC",
$customer_id
));
}
}
class CRM_Admin_Interface {
public function __construct() {
add_action('admin_menu', [$this, 'add_admin_menu']);
add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_scripts']);
}
public function add_admin_menu() {
add_menu_page(
'CRM系统',
'客户关系管理',
'manage_options',
'crm-dashboard',
[$this, 'render_dashboard'],
'dashicons-groups',
30
);
add_submenu_page(
'crm-dashboard',
'客户列表',
'所有客户',
'manage_options',
'crm-customers',
[$this, 'render_customers_list']
);
add_submenu_page(
'crm-dashboard',
'销售漏斗',
'销售漏斗',
'manage_options',
'crm-pipeline',
[$this, 'render_pipeline']
);
}
public function render_dashboard() {
?>
<div class="wrap">
<h1>CRM仪表板</h1>
<div class="crm-stats">
<div class="stat-card">
<h3>总客户数</h3>
<p><?php echo $this->get_total_customers(); ?></p>
</div>
<div class="stat-card">
<h3>本月新增</h3>
<p><?php echo $this->get_monthly_new_customers(); ?></p>
</div>
<div class="stat-card">
<h3>转化率</h3>
<p><?php echo $this->get_conversion_rate(); ?>%</p>
</div>
</div>
</div>
<?php
}
public function enqueue_admin_scripts($hook) {
if (strpos($hook, 'crm-') !== false) {
wp_enqueue_style('crm-admin', plugin_dir_url(__FILE__) . 'assets/admin.css');
wp_enqueue_script('crm-admin', plugin_dir_url(__FILE__) . 'assets/admin.js',
['jquery', 'wp-api'], null, true);
}
}
}
new CRM_Admin_Interface();
class CRM_Email_Automation {
private $mailer;
public function __construct() {
$this->mailer = new CRM_Mailer();
add_action('crm_customer_status_changed', [$this, 'handle_status_change']);
add_action('crm_new_lead', [$this, 'send_welcome_email']);
}
public function handle_status_change($customer_id, $old_status, $new_status) {
$customer = get_userdata($customer_id);
switch ($new_status) {
case 'customer':
$this->send_onboarding_email($customer);
break;
case 'vip':
$this->send_vip_welcome($customer);
break;
case 'inactive':
$this->send_winback_email($customer);
break;
}
}
private function send_onboarding_email($customer) {
$subject = '欢迎成为我们的客户!';
$body = $this->render_template('emails/onboarding.php', [
'customer' => $customer
]);
$this->mailer->send($customer->user_email, $subject, $body);
}
private function render_template($template, $data) {
ob_start();
extract($data);
include(CRM_PATH . 'templates/' . $template);
return ob_get_clean();
}
}
class CRM_Workflow_Engine {
private $triggers = [];
private $actions = [];
public function __construct() {
add_action('init', [$this, 'register_default_triggers']);
add_action('crm_workflow_execute', [$this, 'execute_workflow']);
}
public function register_default_triggers() {
$this->add_trigger('user_registration', '用户注册');
$this->add_trigger('form_submission', '表单提交');
$this->add_trigger('purchase_completed', '购买完成');
$this->add_trigger('content_download', '内容下载');
}
public function add_trigger($slug, $name) {
$this->triggers[$slug] = $name;
}
public function add_action($slug, $callback) {
$this->actions[$slug] = $callback;
}
public function create_workflow($trigger, $conditions, $actions) {
$workflow_id = wp_insert_post([
'post_title' => '自动化工作流',
'post_type' => 'crm_workflow',
'post_status' => 'publish'
]);
update_post_meta($workflow_id, '_trigger', $trigger);
update_post_meta($workflow_id, '_conditions', $conditions);
update_post_meta($workflow_id, '_actions', $actions);
}
public function execute_workflow($workflow_id, $data) {
$trigger = get_post_meta($workflow_id, '_trigger', true);
$conditions = get_post_meta($workflow_id, '_conditions', true);
$actions = get_post_meta($workflow_id, '_actions', true);
if ($this->check_conditions($conditions, $data)) {
$this->execute_actions($actions, $data);
}
}
}
class CRM_Analytics {
public function get_customer_metrics() {
global $wpdb;
return [
'total_customers' => $this->count_total_customers(),
'active_customers' => $this->count_active_customers(),
'conversion_rate' => $this->calculate_conversion_rate(),
'average_value' => $this->calculate_average_order_value(),
'retention_rate' => $this->calculate_retention_rate()
];
}
public function generate_sales_report($start_date, $end_date) {
global $wpdb;
$query = $wpdb->prepare(
"SELECT DATE(created_at) as date,
COUNT(*) as leads,
SUM(CASE WHEN status = 'converted' THEN 1 ELSE 0 END) as conversions
FROM {$wpdb->prefix}crm_leads
WHERE created_at BETWEEN %s AND %s
GROUP BY DATE(created_at)
ORDER BY date",
$start_date, $end_date
);
return $wpdb->get_results($query);
}
public function get_customer_lifetime_value($customer_id) {
$orders = wc_get_orders([
'customer_id' => $customer_id,
'limit' => -1,
'return' => 'ids'
]);
$total_value = 0;
foreach ($orders as $order_id) {
$order = wc_get_order($order_id);
$total_value += $order->get_total();
}
return $total_value;
}
}
class CRM_Data_Visualization {
public function render_dashboard_charts() {
wp_enqueue_script('chart-js', 'https://cdn.jsdelivr.net/npm/chart.js');
$data = $this->prepare_chart_data();
?>
<canvas id="crm-conversions-chart" width="400" height="200"></canvas>
<?php
}
private function prepare_chart_data() {
$analytics = new CRM_Analytics();
$report = $analytics->generate_sales_report(
date('Y-m-01'), // 本月第一天
date('Y-m-d') // 今天
);
$labels = [];
$values = [];
foreach ($report as $day) {
$labels[] = $day->date;
$values[] = $day->leads > 0 ? ($day->conversions / $day->leads) * 100 : 0;
}
return [
'labels' => $labels,
'values' => $values
];
}
}
通过以上完整的CRM系统集成方案,WordPress主题可以转型为强大的客户关系管理平台。这些解决方案涵盖了从基础数据管理到高级自动化工作流的各个方面,为企业提供完整的客户生命周期管理能力。
减价出售
减价出售
减价出售
减价出售
电话咨询
1855-626-3292
微信咨询