-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwoogetaddress.php
113 lines (96 loc) · 3.89 KB
/
woogetaddress.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
<?php
/*
Plugin Name: WooGetAddress
Plugin URI: https://github.com/casserlyprogramming/woogetaddress
Description: Woocommerce Extension for getaddress.io
Version: 1.0.0
Author: Daniel Casserly
Author URI: http://dandalfprogramming.blogspot.co.uk/
*/
if (!defined('ABSPATH'))
exit; // Exit
// Actions
// This one does the action for both shipping and billing - hence the stupid
// code on the client side that the address to both.
add_action('woocommerce_before_edit_account_address_form', 'wga_add_address_lookup');
add_action('woocommerce_before_checkout_billing_form', 'wga_add_address_lookup');
add_action('woocommerce_get_sections_products', 'wga_add_setting_section');
add_action('woocommerce_get_settings_products', 'wga_add_settings', 10, 2);
// Adding the settings
function wga_add_setting_section($sections) {
$sections['wga'] = __('WooGetAddress', 'wga');
return $sections;
}
function wga_add_settings($settings, $current_section){
if($current_section === 'wga') {
$settings_wga = array();
// Add the title to the settings...
$settings_wga[] = array(
'name' => __('Woo Get Address Settings', 'wga'),
'type' => 'title',
'desc' => __('The following options are for the WooGetAddress Plugin', 'wga'),
'id' => 'wga');
$settings_wga[] = array(
'name' => __('API Key', 'wga'),
'desc_tip' => __('Enter you api key from getaddress.io here', 'wga'),
'id' => 'wga_apikey',
'type' => 'text',
'desc' => __('The ApiKey is needed to show the billing and shipping search for the address', 'wga')
);
$settings_wga[] = array('type' => 'sectionend', 'id'=> 'wga');
return $settings_wga;
}
else {
return $settings;
}
}
// Front end code
function wga_add_address_lookup() {
$api_key = get_option('wga_apikey');
?>
<p>
<input type="text" class="input-text" placeholder="Enter postcode"
name="addressSearch" id="addressSearch"/>
<a href="#" class="button" id="btnSearchPC">Search</a>
</p>
<p>
<select id="addressChoices" class="input-text"></select>
</p>
<script type="text/javascript">
jQuery(document).ready(function(){
var options;
// Button click event
jQuery("#btnSearchPC").click(function(){
// TODO: error handling here too!
var uri = 'https://api.getaddress.io/find/' + jQuery("#addressSearch").val() + '?api-key=<?php echo $api_key; ?>';
jQuery.getJSON(uri).done(function(data){
// TODO: error handling here
options = data.addresses;
var optionsHTML;
for(var i = 0; i < data.addresses.length; i++) {
optionsHTML += '<option value="' + i + '">' + data.addresses[i] + "</option>";
}
jQuery("#addressChoices").html(optionsHTML);
});
return false;
});
jQuery("#addressChoices").on('change', function(){
var address = options[this.value];
var addressArray = address.split(', ');
// Billing address
jQuery('#billing_address_1').val(addressArray[0]);
jQuery('#billing_address_2').val(addressArray[1]);
jQuery('#billing_city').val(addressArray[5]);
jQuery('#billing_state').val(addressArray[6]);
jQuery('#billing_postcode').val(jQuery("#addressSearch").val());
// Shipping address
jQuery('#shipping_address_1').val(addressArray[0]);
jQuery('#shipping_address_2').val(addressArray[1]);
jQuery('#shipping_city').val(addressArray[5]);
jQuery('#shipping_state').val(addressArray[6]);
jQuery('#shipping_postcode').val(jQuery("#addressSearch").val());
});
});
</script>
<?php
}