-
Notifications
You must be signed in to change notification settings - Fork 2
/
woocommerce_civicrm_checkout_fill.php
170 lines (143 loc) · 6.12 KB
/
woocommerce_civicrm_checkout_fill.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/*
Plugin Name: Woocommerce CiviCRM Populate Address
Plugin URI:
Description: Plugin for populating Woocommerce checkout details with CiviCRM contact details, it only retrieves data from CiviCRM, it does not write to the database, it must be used in conjuction with Veda Consulting's Woocommerce Integration plugin which handles create/update user's details
Author: Andrei Mondoc (andreimondoc at gmail.com)
Version: 0.1
Author URI:
*/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
add_filter( 'woocommerce_billing_fields' , 'woocommerce_civicrm_populate_address' );
// Hook in Woocommerece's checkout
function woocommerce_civicrm_populate_address( $fields ) {
// Get data only if user is logged in
if ( is_user_logged_in() ) {
// Get current user
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
// Bootstrap CiviCRM
if ( !civicrm_wp_initialize() ) {
return;
}
// Get CiviCRM contact_id
$contact_id = civicrm_api3('UFMatch', 'get', array(
'sequential' => 1,
'uf_id' => $user_id,
));
if ( $contact_id['count'] == 1 && $contact_id['values'] != '' ) {
$contact_id = $contact_id['values'][0]['contact_id'];
// Get Civi contact
$contact_details = civicrm_api3('Contact', 'get', array(
'sequential' => 1,
'id' => $contact_id,
));
$contact_details = $contact_details['values'][0];
// Get billing address
$is_billing = civicrm_api3('Address', 'get', array(
'sequential' => 1,
'contact_id' => $contact_id,
'location_type_id' => 'Billing',
));
$is_billing = $is_billing['values'][0];
// If user has Billing address ($is_billing)
if ( $is_billing['location_type_id'] == 5 ) {
$street_address = $is_billing['street_address'];
$supplemental_address_1 = $is_billing['supplemental_address_1'];
$city = $is_billing['city'];
$postal_code = $is_billing['postal_code'];
$name = $is_billing['name'];
// If there's no Billing name in CiviCRM use Contact's name
if ( empty($name) ) {
$name = array( 0 => $contact_details['first_name'], 1 => $contact_details['last_name'] );
} else {
$name = explode(' ', $name);
}
$country = civicrm_api3('Country', 'get', array(
'sequential' => 1,
'id' => $is_billing['country_id'],
));
$country = $country['values'][0]['iso_code'];
// get State/Province abbreviation, as theres no API for it we use CRM_Core_DAO
if ( !empty($is_billing['state_province_id']) ) {
$query = "SELECT name FROM civicrm_state_province WHERE id = %1";
$params = array(
1 => array($is_billing['state_province_id'], 'Integer'));
$state_province_abbr = CRM_Core_DAO::singleValueQuery($query, $params);
}
} else {
return $fields;
}
// Update woocommerce meta data before the form is loaded
update_user_meta( $user_id, 'billing_first_name', $name[0] );
update_user_meta( $user_id, 'billing_last_name', $name[1] );
update_user_meta( $user_id, 'billing_address_1', $street_address );
update_user_meta( $user_id, 'billing_address_2', $supplemental_address_1 );
update_user_meta( $user_id, 'billing_city', $city );
update_user_meta( $user_id, 'billing_postcode', $postal_code );
update_user_meta( $user_id, 'billing_country', $country );
update_user_meta( $user_id, 'billing_state', $state_province_abbr);
}
}
return $fields;
}
// Update/Create billing address form Woocommerce's My-Account page
add_action( 'woocommerce_customer_save_address', 'woocommerce_civicrm_update_civi_address', 20 );
function woocommerce_civicrm_update_civi_address( $user_id ) {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
// Get all user meta data
$user_meta = array_map( function( $a ){ return $a[0]; }, get_user_meta( $user_id ) );
//$billing_address_1 = get_user_meta($user_id, 'billing_address_1', true);
$contact_id = civicrm_api3('UFMatch', 'get', array(
'sequential' => 1,
'uf_id' => $user_id,
));
$contact_id = $contact_id['values'][0]['contact_id'];
$existing_billing = civicrm_api3('Address', 'get', array(
'sequential' => 1,
'contact_id' => $contact_id,
'location_type_id' => 'Billing',
));
//$existing_billing = $existing_billing['values'][0];
// State/Province is a required field in Woocommerce
$query = "SELECT id FROM civicrm_state_province WHERE abbreviation = %1";
$params = array(
1 => array($user_meta['billing_state'], 'String'));
$state_province_id = CRM_Core_DAO::singleValueQuery($query, $params);
if ( $existing_billing['count'] != 0 && $existing_billing['values'] != '' ) {
$country_id = civicrm_api3('Country', 'get', array(
'sequential' => 1,
'iso_code' => $user_meta['billing_country'],
));
// Update existing billing address
$is_billing = civicrm_api3('Address', 'create', array(
'sequential' => 1,
'contact_id' => $contact_id,
'id' => $existing_billing['id'],
'street_address' => $user_meta['billing_address_1'],
'supplemental_address_1' => $user_meta['billing_address_2'],
'city' => $user_meta['billing_city'],
'postal_code' => $user_meta['billing_postcode'],
'name' => $user_meta['billing_first_name'].' '.$user_meta['billing_last_name'],
'country_id' => $country_id['id'],
'state_province_id' => $state_province_id,
));
} else {
// create new Address
$is_billing = civicrm_api3('Address', 'create', array(
'sequential' => 1,
'contact_id' => $contact_id,
'location_type_id' => 'Billing',
'street_address' => $user_meta['billing_address_1'],
'supplemental_address_1' => $user_meta['billing_address_2'],
'city' => $user_meta['billing_city'],
'postal_code' => $user_meta['billing_postcode'],
'name' => $user_meta['billing_first_name'].' '.$user_meta['billing_last_name'],
'country_id' => $country_id['id'],
'state_province_id' => $state_province_id,
));
}
}
}
?>