forked from woocommerce/woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwoocommerce_emails.php
413 lines (287 loc) · 11.1 KB
/
woocommerce_emails.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php
/**
* WooCommerce Emails
*
* Email handling for important shop events.
*
* @package WooCommerce
* @category Emails
* @author WooThemes
*/
/**
* Mail from name/email
**/
function woocommerce_mail_from_name( $name ) {
return get_option('woocommerce_email_from_name');
}
function woocommerce_mail_from( $email ) {
return get_option('woocommerce_email_from_address');
}
/**
* HTML emails from WooCommerce
**/
function woocommerce_mail( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = "" ) {
add_filter( 'wp_mail_from', 'woocommerce_mail_from' );
add_filter( 'wp_mail_from_name', 'woocommerce_mail_from_name' );
add_filter( 'wp_mail_content_type', 'woocommerce_email_content_type' );
// Send the mail
wp_mail( $to, $subject, $message, $headers, $attachments );
// Unhook
remove_filter( 'wp_mail_from', 'woocommerce_mail_from' );
remove_filter( 'wp_mail_from_name', 'woocommerce_mail_from_name' );
remove_filter( 'wp_mail_content_type', 'woocommerce_email_content_type' );
}
/**
* Wraps a message in the woocommerce mail template
**/
function woocommerce_mail_template( $heading, $message ) {
global $email_heading;
$email_heading = $heading;
// Buffer
ob_start();
do_action('woocommerce_email_header');
echo wpautop(wptexturize( $message ));
do_action('woocommerce_email_footer');
// Get contents
$message = ob_get_clean();
return $message;
}
/**
* Email Header
**/
add_action('woocommerce_email_header', 'woocommerce_email_header');
function woocommerce_email_header() {
woocommerce_get_template('emails/email_header.php', false);
}
/**
* Email Footer
**/
add_action('woocommerce_email_footer', 'woocommerce_email_footer');
function woocommerce_email_footer() {
woocommerce_get_template('emails/email_footer.php', false);
}
/**
* HTML email type
**/
function woocommerce_email_content_type($content_type){
return 'text/html';
}
/**
* Fix recieve password mail links
**/
function woocommerce_retrieve_password_message($content){
return htmlspecialchars($content);
}
/**
* Hooks for emails
**/
add_action('woocommerce_low_stock_notification', 'woocommerce_low_stock_notification');
add_action('woocommerce_no_stock_notification', 'woocommerce_no_stock_notification');
add_action('woocommerce_product_on_backorder_notification', 'woocommerce_product_on_backorder_notification', 1, 2);
/**
* New order notification email template
**/
add_action('woocommerce_order_status_pending_to_processing', 'woocommerce_new_order_notification');
add_action('woocommerce_order_status_pending_to_completed', 'woocommerce_new_order_notification');
add_action('woocommerce_order_status_pending_to_on-hold', 'woocommerce_new_order_notification');
add_action('woocommerce_order_status_failed_to_processing', 'woocommerce_new_order_notification');
add_action('woocommerce_order_status_failed_to_completed', 'woocommerce_new_order_notification');
function woocommerce_new_order_notification( $id ) {
global $order_id, $email_heading;
$order_id = $id;
$email_heading = __('New Customer Order', 'woothemes');
$subject = sprintf(__('[%s] New Customer Order (# %s)', 'woothemes'), get_bloginfo('name'), $order_id);
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/new_order.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
woocommerce_mail( get_option('woocommerce_new_order_email_recipient'), $subject, $message );
}
/**
* Processing order notification email template
**/
add_action('woocommerce_order_status_pending_to_processing', 'woocommerce_processing_order_customer_notification');
add_action('woocommerce_order_status_pending_to_on-hold', 'woocommerce_processing_order_customer_notification');
function woocommerce_processing_order_customer_notification( $id ) {
global $order_id, $email_heading;
$order_id = $id;
$order = &new woocommerce_order( $order_id );
$email_heading = __('Order Received', 'woothemes');
$subject = sprintf(__('[%s] Order Received', 'woothemes'), get_bloginfo('name'));
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/customer_processing_order.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
woocommerce_mail( $order->billing_email, $subject, $message );
}
/**
* Completed order notification email template - this one includes download links for downloadable products
**/
add_action('woocommerce_order_status_completed', 'woocommerce_completed_order_customer_notification');
function woocommerce_completed_order_customer_notification( $id ) {
global $order_id, $email_heading;
$order_id = $id;
$order = &new woocommerce_order( $order_id );
if ($order->has_downloadable_item()) :
$subject = __('[%s] Order Complete/Download Links', 'woothemes');
$email_heading = __('Order Complete/Download Links', 'woothemes');
else :
$subject = __('[%s] Order Complete', 'woothemes');
$email_heading = __('Order Complete', 'woothemes');
endif;
$email_heading = apply_filters('woocommerce_completed_order_customer_notification_subject', $email_heading);
$subject = sprintf($subject, get_bloginfo('name'));
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/customer_completed_order.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
woocommerce_mail( $order->billing_email, $subject, $message );
}
/**
* Pay for order notification email template - this one includes a payment link
**/
function woocommerce_pay_for_order_customer_notification( $the_order ) {
global $order_id, $order, $email_heading;
$order = $the_order;
$order_id = $order->id;
$email_heading = sprintf(__('Invoice for Order #%s', 'woothemes'), $order_id);
$subject = sprintf(__('[%s] Pay for Order', 'woothemes'), get_bloginfo('name'));
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/customer_pay_for_order.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
woocommerce_mail( $order->billing_email, $subject, $message );
}
/**
* Customer note notification
**/
add_action('woocommerce_new_customer_note', 'woocommerce_customer_note_notification', 10, 2);
function woocommerce_customer_note_notification( $id, $note ) {
global $order_id, $email_heading, $customer_note;
$order_id = $id;
$customer_note = $note;
$order = &new woocommerce_order( $order_id );
if (!$customer_note) return;
$email_heading = __('A note has been added to your order', 'woothemes');
$subject = sprintf(__('[%s] A note has been added to your order', 'woothemes'), get_bloginfo('name'));
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/customer_note_notification.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
woocommerce_mail( $order->billing_email, $subject, $message );
}
/**
* Low stock notification email
**/
function woocommerce_low_stock_notification( $product ) {
$_product = &new woocommerce_product($product);
$subject = '[' . get_bloginfo('name') . '] ' . __('Product low in stock', 'woothemes');
$message = woocommerce_mail_template(
__('Product low in stock', 'woothemes'),
'#' . $_product->id .' '. $_product->get_title() . ' ('. $_product->sku.') ' . __('is low in stock.', 'woothemes')
);
// Send the mail
woocommerce_mail( get_option('woocommerce_stock_email_recipient'), $subject, $message );
}
/**
* No stock notification email
**/
function woocommerce_no_stock_notification( $product ) {
$_product = &new woocommerce_product($product);
$subject = '[' . get_bloginfo('name') . '] ' . __('Product out of stock', 'woothemes');
$message = woocommerce_mail_template(
__('Product out of stock', 'woothemes'),
'#' . $_product->id .' '. $_product->get_title() . ' ('. $_product->sku.') ' . __('is out of stock.', 'woothemes')
);
// Send the mail
woocommerce_mail( get_option('woocommerce_stock_email_recipient'), $subject, $message );
}
/**
* Backorder notification email
**/
function woocommerce_product_on_backorder_notification( $product, $amount ) {
$_product = &new woocommerce_product($product);
$subject = '[' . get_bloginfo('name') . '] ' . __('Product Backorder', 'woothemes');
$message = woocommerce_mail_template(
__('Product Backorder', 'woothemes'),
$amount . __(' units of #', 'woothemes') . $_product->id .' '. $_product->get_title() . ' ('. $_product->sku.') ' . __('have been backordered.', 'woothemes')
);
// Send the mail
woocommerce_mail( get_option('woocommerce_stock_email_recipient'), $subject, $message );
}
/**
* Preview Emails
**/
add_action('admin_init', 'woocommerce_preview_emails');
function woocommerce_preview_emails() {
if (isset($_GET['preview_woocommerce_mail'])) :
$nonce = $_REQUEST['_wpnonce'];
if (!wp_verify_nonce($nonce, 'preview-mail') ) die('Security check');
global $email_heading;
$email_heading = __('Email preview', 'woothemes');
do_action('woocommerce_email_header');
echo '<h2>WooCommerce sit amet</h2>';
echo wpautop('Ut ut est qui euismod parum. Dolor veniam tation nihil assum mazim. Possim fiant habent decima et claritatem. Erat me usus gothica laoreet consequat. Clari facer litterarum aliquam insitam dolor.
Gothica minim lectores demonstraverunt ut soluta. Sequitur quam exerci veniam aliquip litterarum. Lius videntur nisl facilisis claritatem nunc. Praesent in iusto me tincidunt iusto. Dolore lectores sed putamus exerci est. ');
do_action('woocommerce_email_footer');
exit;
endif;
}
/**
* Add order meta to email templates
**/
add_action('woocommerce_email_after_order_table', 'woocommerce_email_order_meta', 10, 2);
function woocommerce_email_order_meta( $order, $sent_to_admin ) {
$meta = array();
$show_fields = apply_filters('woocommerce_email_order_meta_keys', array('coupons'), $sent_to_admin);
if ($order->customer_note) :
$meta[__('Note:', 'woothemes')] = wptexturize($order->customer_note);
endif;
if ($show_fields) foreach ($show_fields as $field) :
$value = get_post_meta( $order->id, $field, true );
if ($value) $meta[ucwords(esc_attr($field))] = wptexturize($value);
endforeach;
if (sizeof($meta)>0) :
echo '<h2>'.__('Order information', 'woothemes').'</h2>';
foreach ($meta as $key=>$value) :
echo '<p><strong>'.$key.':</strong> '.$value.'</p>';
endforeach;
endif;
}
/**
* Customer new account welcome email
**/
function woocommerce_customer_new_account( $user_id, $plaintext_pass ) {
global $email_heading, $user_login, $user_pass, $blogname;
if ( empty($plaintext_pass) ) return;
$user = new WP_User($user_id);
$user_login = stripslashes($user->user_login);
$user_email = stripslashes($user->user_email);
$user_pass = $plaintext_pass;
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$subject = sprintf(__('Your account on %s', 'woothemes'), $blogname);
$email_heading = __('Your account details', 'woothemes');
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/customer_new_account.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
woocommerce_mail( $user_email, $subject, $message );
}