This repository has been archived by the owner on Oct 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
woocommerce-order-notify.php
196 lines (166 loc) · 6.41 KB
/
woocommerce-order-notify.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<?php
/*
Plugin Name: WooCommerce Order Notify
Description: Notify custom app on event.
Author: S.Z.
License: GPL2
*/
/**
* Class WC_Order_Notify_Plugin.
*
* @author S.Z. <s****.z****@gmail.com>
*/
class WC_Order_Notify_Plugin
{
/**
* Constructor.
*/
public function __construct()
{
// When status got completed
add_action('woocommerce_order_status_completed', [$this, 'on_completed'], 10, 2);
// When any data are updated
add_action('woocommerce_process_shop_order_meta', [$this, 'on_order_update'], 10, 2);
// When initialized
add_action('admin_init', [$this, 'woocommerce_order_notify_settings_init']);
// When menu load
add_action('admin_menu', [$this, 'woocommerce_order_notify_add_admin_menu']);
}
public function woocommerce_order_notify_add_admin_menu()
{
add_options_page('Woocommerce Order Notify', 'Woocommerce Order Notify',
'manage_options', 'Woocommerce Order Notify',
[$this, 'woocommerce_order_notify_options_page']);
}
public function woocommerce_order_notify_settings_init()
{
register_setting('woocommerce_order_notify_settings_page', 'woocommerce_order_notify_settings');
add_settings_section(
'woocommerce_order_notify_plugin_page_section',
__('NationBuilder\'s settings', 'Woocommerce Order Notify'),
[$this, 'woocommerce_order_notify_settings_section_callback'],
'woocommerce_order_notify_settings_page'
);
add_settings_field(
'woocommerce_order_notify_api_key',
__('API Key', 'Woocommerce Order Notify'),
[$this, 'woocommerce_order_notify_api_key_render'],
'woocommerce_order_notify_settings_page',
'woocommerce_order_notify_plugin_page_section'
);
add_settings_field(
'woocommerce_order_notify_nation_slug',
__('Nation slug', 'Woocommerce Order Notify'),
[$this, 'woocommerce_order_notify_nation_slug_render'],
'woocommerce_order_notify_settings_page',
'woocommerce_order_notify_plugin_page_section'
);
add_settings_field(
'woocommerce_order_notify_tag_name',
__('Tag name', 'Woocommerce Order Notify'),
[$this, 'woocommerce_order_notify_tag_name_render'],
'woocommerce_order_notify_settings_page',
'woocommerce_order_notify_plugin_page_section'
);
}
public function woocommerce_order_notify_api_key_render()
{
$options = get_option('woocommerce_order_notify_settings'); ?>
<input type='text'
name='woocommerce_order_notify_settings[woocommerce_order_notify_api_key]'
value='<?php echo $options['woocommerce_order_notify_api_key']; ?>'>
<?php
}
public function woocommerce_order_notify_nation_slug_render()
{
$options = get_option('woocommerce_order_notify_settings'); ?>
<input type='text'
name='woocommerce_order_notify_settings[woocommerce_order_notify_nation_slug]'
value='<?php echo $options['woocommerce_order_notify_nation_slug']; ?>'>
<?php
}
public function woocommerce_order_notify_tag_name_render()
{
$options = get_option('woocommerce_order_notify_settings'); ?>
<input type='text'
name='woocommerce_order_notify_settings[woocommerce_order_notify_tag_name]'
value='<?php echo $options['woocommerce_order_notify_tag_name']; ?>'>
<?php
}
public function woocommerce_order_notify_settings_section_callback()
{
echo __('please register your NationBuilder\'s settings',
'Woocommerce Order Notify');
}
public function woocommerce_order_notify_options_page()
{
?>
<form action='options.php' method='post'>
<h2>Woocommerce Order Notify</h2>
<?php
settings_fields('woocommerce_order_notify_settings_page');
do_settings_sections('woocommerce_order_notify_settings_page');
submit_button();
?>
</form>
<?php
}
/**
* When order status change to completed.
*
* @param WC_Order $order
*/
public function on_completed($order_id)
{
$options = get_option('woocommerce_order_notify_settings');
$order = wc_get_order($order_id);
$url = 'https://'.$options['woocommerce_order_notify_nation_slug'].
'.nationbuilder.com/api/v1/people/match?email='.
urlencode($order->billing_email).'&access_token='.
$options['woocommerce_order_notify_api_key'];
$response = wp_remote_get($url, [
'headers' => ['Accept' => 'application/json'],
'httpversion' => '1.1',
'user-agent' => '',
]);
if (is_wp_error($response)) {
error_log($response->get_error_message());
return ;
}
if (json_decode($response['body'])->code === 'no_matches') {
$url = 'https://'.$options['woocommerce_order_notify_nation_slug'].
'.nationbuilder.com/api/v1/people?access_token='.
$options['woocommerce_order_notify_api_key'];
$response = wp_remote_post( $url, [
'headers' => [
'Accept' => 'application/json',
'Content-type' => 'application/json'
],
'httpversion' => '1.1',
'user-agent' => '',
'body' => '{"person":{"email":"'.$order->billing_email.'"}}',
]);
}
if (is_wp_error($response)) {
error_log($response->get_error_message());
return ;
}
$id = json_decode($response['body'])->person->id;
if ($id) {
$url = 'https://'.$options['woocommerce_order_notify_nation_slug'].
'.nationbuilder.com/api/v1/people/'.$id.'/taggings?access_token='.
$options['woocommerce_order_notify_api_key'];
wp_remote_request($url, [
'method' => 'PUT',
'httpversion' => '1.1',
'user-agent' => '',
'headers' => [
'content-type' => 'application/json',
'Accept' => 'application/json'
],
'body' => '{"tagging":{"tag": "'.$options['woocommerce_order_notify_tag_name'].'"}}',
]);
}
}
}
new WC_Order_Notify_Plugin();