jigoshop /jigoshop_emails.php

Language PHP Lines 516
MD5 Hash a3fa41d661ce8e51f82d2b5d04464a92
Repository https://github.com/scottpoulin/jigoshop.git View Raw File
  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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
<?php
/**
 * Jigoshop Emails
 *
 * DISCLAIMER
 *
 * Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
 * versions in the future. If you wish to customise Jigoshop core for your needs,
 * please use our GitHub repository to publish essential changes for consideration.
 *
 * @package             Jigoshop
 * @category            Core
 * @author              Jigoshop
 * @copyright           Copyright Š 2011-2013 Jigoshop.
 * @license             http://jigoshop.com/license/commercial-edition
 */
/**
 * Hooks for emails
 * */
add_action('jigoshop_low_stock_notification', 'jigoshop_low_stock_notification');
add_action('jigoshop_no_stock_notification', 'jigoshop_no_stock_notification');
add_action('jigoshop_product_on_backorder_notification', 'jigoshop_product_on_backorder_notification', 1, 3);


/**
 * New order notification email template
 * */
add_action('order_status_pending_to_processing', 'jigoshop_new_order_notification');
add_action('order_status_pending_to_completed', 'jigoshop_new_order_notification');
add_action('order_status_pending_to_on-hold', 'jigoshop_new_order_notification');

function jigoshop_new_order_notification($order_id) {

	$jigoshop_options = Jigoshop_Base::get_options();
	$order = new jigoshop_order($order_id);

	$subject = html_entity_decode(sprintf(__('[%s] New Customer Order (%s)', 'jigoshop'), get_bloginfo('name'), $order->get_order_number()), ENT_QUOTES, 'UTF-8');

	ob_start();

	echo __("You have received an order from ", 'jigoshop') . $order->billing_first_name . ' ' . $order->billing_last_name . __(". Their order is as follows:", 'jigoshop') . PHP_EOL . PHP_EOL;

	add_header_info($order);

	add_order_totals($order, false, true);

	add_customer_details($order);

	add_billing_address_details($order);

	add_shipping_address_details($order);

	$message = ob_get_clean();

	$message = apply_filters('jigoshop_change_new_order_email_contents', $message, $order);
	$message = html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8');

	wp_mail($jigoshop_options->get_option('jigoshop_email'), $subject, $message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");
}

/**
 * Processing order notification email template
 * */
add_action('order_status_pending_to_processing', 'jigoshop_processing_order_customer_notification');
add_action('order_status_pending_to_on-hold', 'jigoshop_processing_order_customer_notification');

function jigoshop_processing_order_customer_notification($order_id) {

	$jigoshop_options = Jigoshop_Base::get_options();
	$order = new jigoshop_order($order_id);

	$subject = html_entity_decode('[' . get_bloginfo('name') . '] ' . __('Order Received', 'jigoshop'), ENT_QUOTES, 'UTF-8');

	ob_start();
	echo __("Thank you, we are now processing your order. Your order's details are below:", 'jigoshop') . PHP_EOL . PHP_EOL;

	add_header_info($order);

	add_order_totals($order, false, true);

	if (strtolower($order->payment_method) == "bank_transfer") :

		echo add_email_separator( '-' ) . PHP_EOL;
		echo __('BANK PAYMENT DETAILS', 'jigoshop') . PHP_EOL;
		echo add_email_separator( '-' ) . PHP_EOL;

		echo jigoshop_bank_transfer::get_bank_details();

		echo PHP_EOL;

		do_action('jigoshop_after_email_bank_payment_details', $order->id);

	endif;

	add_customer_details($order);

	add_billing_address_details($order);

	add_shipping_address_details($order);

	$message = ob_get_clean();

	$message = apply_filters('jigoshop_change_processing_order_email_contents', $message, $order);
	$message = html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8');

	wp_mail($order->billing_email, $subject, $message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");
}

/**
 * Completed order notification email template - this one includes download links for downloadable products
 * */
add_action('order_status_completed', 'jigoshop_completed_order_customer_notification');

function jigoshop_completed_order_customer_notification($order_id) {

	$jigoshop_options = Jigoshop_Base::get_options();
	$order = new jigoshop_order($order_id);

	$subject = html_entity_decode('[' . get_bloginfo('name') . '] ' . __('Order Complete', 'jigoshop'), ENT_QUOTES, 'UTF-8');

	ob_start();
	echo __("Your order is complete. Your order's details are below:", 'jigoshop') . PHP_EOL . PHP_EOL;

	add_header_info($order);

	$download_links =  apply_filters('jigoshop_download_links_on_completed',true);
	add_order_totals($order, true, true);

	add_customer_details($order);

	add_billing_address_details($order);

	add_shipping_address_details($order);

	$message = ob_get_clean();


	$message = apply_filters('jigoshop_change_completed_order_email_contents', $message, $order);
	$message = html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8');
	$message = apply_filters('jigoshop_completed_order_customer_notification_mail_message', $message);

	wp_mail($order->billing_email, $subject, $message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");
}

/**
 * Refunded order notification email template - this one does not include download links for downloadable products
 * */
add_action('order_status_refunded', 'jigoshop_refunded_order_customer_notification');

function jigoshop_refunded_order_customer_notification($order_id) {

	$jigoshop_options = Jigoshop_Base::get_options();
	$order = new jigoshop_order($order_id);

	$subject = html_entity_decode('[' . get_bloginfo('name') . '] ' . __('Order Refunded', 'jigoshop'), ENT_QUOTES, 'UTF-8');

	ob_start();
	echo __("Your order has been refunded. Your order's details are below:", 'jigoshop') . PHP_EOL . PHP_EOL;

	add_header_info($order);

	add_order_totals($order, false, true);

	add_customer_details($order);

	add_billing_address_details($order);

	add_shipping_address_details($order);

	$message = ob_get_clean();

	$message = apply_filters('jigoshop_change_refunded_email_message', $message, $order);
	$message = html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8');
	$message = apply_filters('jigoshop_refunded_order_customer_notification_mail_message', $message);

	wp_mail($order->billing_email, $subject, $message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");
}

/**
 * Customer invoice for an order.
 *
 * Displays link for payment if the order is marked pending.
 * Includes download link if order is completed.
 * */
function jigoshop_send_customer_invoice($order_id) {

	$jigoshop_options = Jigoshop_Base::get_options();
	$order = new jigoshop_order($order_id);

	$subject = html_entity_decode('[' . get_bloginfo('name') . '] ' . sprintf(__('Invoice for Order %s', 'jigoshop'), $order->get_order_number()), ENT_QUOTES, 'UTF-8');

	$customer_message = '';
	if ($order->status == 'pending') :
		$customer_message = sprintf(__("An order has been created for you on &quot;%s&quot;. To pay for this order please use the following link: %s", 'jigoshop') . PHP_EOL . PHP_EOL, get_bloginfo('name'), $order->get_checkout_payment_url());
	endif;

	ob_start();
	add_header_info($order);

	if ($order->status == 'completed') :
		$download_links = apply_filters('jigoshop_download_links_on_invoice',true);
		add_order_totals($order, true, true);
	else :
		add_order_totals($order, false, true);
	endif;

	$message = ob_get_clean();

	$message = apply_filters('jigoshop_change_pay_order_email_contents', $message, $order);
	$customer_message = html_entity_decode(strip_tags($customer_message . $message), ENT_QUOTES, 'UTF-8');

	wp_mail($order->billing_email, $subject, $customer_message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");
}

function add_header_info($order) {

	echo add_email_separator( '=' ) . PHP_EOL;
	add_company_information();
	
	$info = __('ORDER ', 'jigoshop') . $order->get_order_number();
	$date = __('Date: ','jigoshop') . date_i18n( get_option('date_format') );
	$info .= add_padding_to_email_lines( 80 - strlen( $date ) - strlen( $info ) );
	$info .= $date;
	echo $info . PHP_EOL;
	echo add_email_separator( '=' ) . PHP_EOL;

}

function add_email_separator( $char ) {
	$sep = '';
	for ( $i = 0 ; $i < 80 ; $i++ ) {
		$sep .= $char;
	}
	return $sep;
}

function add_padding_to_email_lines( $amount ) {
	$padding = '';
	for ( $i = 0 ; $i < $amount ; $i++ ) {
		$padding .= ' ';
	}
	return $padding;
}

function add_company_information() {

	$jigoshop_options = Jigoshop_Base::get_options();
	$add_eol = false;

	if ($jigoshop_options->get_option('jigoshop_company_name')) :
		echo $jigoshop_options->get_option('jigoshop_company_name') . PHP_EOL;
		$add_eol = true;
	endif;

	if ($jigoshop_options->get_option('jigoshop_address_line1')) :
		$add_eol = true;
		echo $jigoshop_options->get_option('jigoshop_address_line1') . PHP_EOL;
		if ($jigoshop_options->get_option('jigoshop_address_line2')) :
			echo $jigoshop_options->get_option('jigoshop_address_line2') . PHP_EOL;
		endif;
	endif;

	if ($jigoshop_options->get_option('jigoshop_company_phone')) :
		$add_eol = true;
		echo $jigoshop_options->get_option('jigoshop_company_phone') . PHP_EOL;
	endif;

	if ($jigoshop_options->get_option('jigoshop_company_email')) :
		$add_eol = true;
		echo '<a href="mailto:' . $jigoshop_options->get_option('jigoshop_company_email') . '">' . $jigoshop_options->get_option('jigoshop_company_email') . '</a>' . PHP_EOL;
	endif;

	if ($add_eol) echo PHP_EOL;

}

function add_order_totals($order, $show_download_links, $show_sku) {

	do_action('jigoshop_before_email_order_info', $order->id);  
  
	$jigoshop_options = Jigoshop_Base::get_options();
	$inc_tax = ($jigoshop_options->get_option('jigoshop_calc_taxes') == 'no')||($jigoshop_options->get_option('jigoshop_prices_include_tax') == 'yes');
	
	echo PHP_EOL;
	echo $order->email_order_items_list($show_download_links, $show_sku, $inc_tax);

	if ( $order->customer_note ) {
		echo PHP_EOL . __('Note:', 'jigoshop') . $order->customer_note . PHP_EOL;
	}

	if (   ( $jigoshop_options->get_option('jigoshop_calc_taxes') == 'yes' && $order->has_compound_tax() )
		|| ( $jigoshop_options->get_option('jigoshop_tax_after_coupon') == 'yes' && $order->order_discount > 0) ) {
		
		echo PHP_EOL;
		$info = __('Retail Price:', 'jigoshop');
		$info .= add_padding_to_email_lines( 30 - strlen( $info ) );
		$info .= html_entity_decode($order->get_subtotal_to_display(), ENT_QUOTES, 'UTF-8');
		echo $info . PHP_EOL;

	} else {
		
		echo PHP_EOL;
		$info = __('Subtotal:', 'jigoshop');
		$info .= add_padding_to_email_lines( 30 - strlen( $info ) );
		$info .= html_entity_decode($order->get_subtotal_to_display(), ENT_QUOTES, 'UTF-8');
		echo $info . PHP_EOL;
		
	}
	
	if ( $order->order_shipping > 0 ) {
		$info = __('Shipping:', 'jigoshop');
		$info .= add_padding_to_email_lines( 30 - strlen( $info ) );
		$info .= html_entity_decode($order->get_shipping_to_display(), ENT_QUOTES, 'UTF-8');
		echo $info . PHP_EOL;
		
	}
	
	do_action('jigoshop_email_order_professing_fee_info', $order->id);
	
	if ( $jigoshop_options->get_option('jigoshop_tax_after_coupon') == 'yes' && $order->order_discount > 0 ) {
		$info = __('Discount:', 'jigoshop');
		$info .= add_padding_to_email_lines( 30 - strlen( $info ) );
		$info .= html_entity_decode(jigoshop_price($order->order_discount), ENT_QUOTES, 'UTF-8');
		echo $info . PHP_EOL;
		
	}
	
// 	if (   ($jigoshop_options->get_option('jigoshop_calc_taxes') == 'yes' && $order->has_compound_tax())
// 		|| ($jigoshop_options->get_option('jigoshop_tax_after_coupon') == 'yes' && $order->order_discount > 0)) {
// 		
// 		$info = __('Subtotal:', 'jigoshop');
// 		$info .= add_padding_to_email_lines( 30 - strlen( $info ) );
// 		$info .= html_entity_decode(jigoshop_price($order->order_discount_subtotal), ENT_QUOTES, 'UTF-8');
// 		echo $info . PHP_EOL;
// 
// 	}
	
	if ( $jigoshop_options->get_option('jigoshop_calc_taxes') == 'yes') {
		foreach ($order->get_tax_classes() as $tax_class) {
			if ($order->show_tax_entry($tax_class)) {
			
				$info = $order->get_tax_class_for_display($tax_class) . ' (' . (float) $order->get_tax_rate($tax_class) . '%):';
				$info .= add_padding_to_email_lines( 30 - strlen( $info ) );
				$info .= html_entity_decode($order->get_tax_amount($tax_class), ENT_QUOTES, 'UTF-8');
				echo $info . PHP_EOL;

			}
		}
	}
	
	if ( $jigoshop_options->get_option('jigoshop_tax_after_coupon') == 'no' && $order->order_discount > 0 ) {
		
		$info = __('Discount:', 'jigoshop');
		$info .= add_padding_to_email_lines( 30 - strlen( $info ) );
		$info .= html_entity_decode(jigoshop_price($order->order_discount), ENT_QUOTES, 'UTF-8');
		echo $info . PHP_EOL;

	}
	
	$method = $order->payment_method_title <> '' ? ucwords($order->payment_method_title) : __("Free",'jigoshop');
	$info = __('Total:', 'jigoshop');
	$info .= add_padding_to_email_lines( 30 - strlen( $info ) );
	$info .= html_entity_decode(jigoshop_price($order->order_total), ENT_QUOTES, 'UTF-8');
	$info .= ' - ' . __('via', 'jigoshop') . ' ' . $method;
	echo $info . PHP_EOL . PHP_EOL;

	if ($jigoshop_options->get_option('jigoshop_calc_taxes') && $jigoshop_options->get_option('jigoshop_tax_number')) :
		echo $jigoshop_options->get_option('jigoshop_tax_number') . PHP_EOL . PHP_EOL;
	endif;

	do_action('jigoshop_after_email_order_info', $order->id);

}

function add_customer_details($order) {

	echo add_email_separator( '-' ) . PHP_EOL;
	echo __('CUSTOMER DETAILS', 'jigoshop') . PHP_EOL;
	echo add_email_separator( '-' ) . PHP_EOL;

	if ($order->billing_email) {
		$temp = __('Email:', 'jigoshop');
		echo $temp . add_padding_to_email_lines( 30 - strlen( $temp ) ) . $order->billing_email . PHP_EOL;
	}
	if ($order->billing_phone) {
		$temp = __('Tel:', 'jigoshop');
		echo $temp . add_padding_to_email_lines( 30 - strlen( $temp ) ) . $order->billing_phone . PHP_EOL;
	}
	if ( $order->billing_euvatno ) {
		$temp = __('EU VAT Number:', 'jigoshop');
		echo $temp . add_padding_to_email_lines( 30 - strlen( $temp ) ) . $order->billing_euvatno . PHP_EOL;
	}
	echo PHP_EOL;

	do_action('jigoshop_after_email_customer_details', $order->id);

}

function add_billing_address_details($order) {

	echo add_email_separator( '-' ) . PHP_EOL;
	echo __('BILLING ADDRESS', 'jigoshop') . PHP_EOL;
	echo add_email_separator( '-' ) . PHP_EOL;

	echo $order->billing_first_name . ' ' . $order->billing_last_name . PHP_EOL;
	if ($order->billing_company)
		echo $order->billing_company . PHP_EOL;
	echo $order->formatted_billing_address . PHP_EOL . PHP_EOL;
	
	do_action('jigoshop_after_email_billing_address', $order->id);

}

function add_shipping_address_details($order) {

	echo add_email_separator( '-' ) . PHP_EOL;
	echo __('SHIPPING ADDRESS', 'jigoshop') . PHP_EOL;
	echo add_email_separator( '-' ) . PHP_EOL;

	if ( $order->shipping_method != 'local_pickup' ) {

		echo $order->shipping_first_name . ' ' . $order->shipping_last_name . PHP_EOL;
		if ($order->shipping_company) echo $order->shipping_company . PHP_EOL;
		echo $order->formatted_shipping_address . PHP_EOL . PHP_EOL;

		echo __('Shipping: ','jigoshop') . html_entity_decode(ucwords($order->shipping_service), ENT_QUOTES, 'UTF-8') . PHP_EOL . PHP_EOL;

		do_action('jigoshop_after_email_shipping_address', $order->id);

	} else {

		echo __('To be picked up by:', 'jigoshop') . PHP_EOL;
		echo $order->shipping_first_name . ' ' . $order->shipping_last_name . PHP_EOL;
		if ($order->shipping_company) echo $order->shipping_company . PHP_EOL;
		echo PHP_EOL;
		echo __('At location:', 'jigoshop') . PHP_EOL;
		echo add_company_information() . PHP_EOL . PHP_EOL;

	}

}

/**
 * Low stock notification email
 * */
function jigoshop_low_stock_notification($_product) {
	$jigoshop_options = Jigoshop_Base::get_options();
	$subject = html_entity_decode('[' . get_bloginfo('name') . '] ' . __('Product low in stock', 'jigoshop'), ENT_QUOTES, 'UTF-8');
	$message = '#' . $_product->id . ' ' . $_product->get_title() . ' (' . $_product->sku . ') ' . __('is low in stock.', 'jigoshop');
	$message = wordwrap(html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8'), 70);
	wp_mail($jigoshop_options->get_option('jigoshop_email'), $subject, $message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");
}

/**
 * No stock notification email
 * */
function jigoshop_no_stock_notification($_product) {
	$jigoshop_options = Jigoshop_Base::get_options();
	$subject = html_entity_decode('[' . get_bloginfo('name') . '] ' . __('Product out of stock', 'jigoshop'), ENT_QUOTES, 'UTF-8');
	$message = '#' . $_product->id . ' ' . $_product->get_title() . ' (' . $_product->sku . ') ' . __('is out of stock.', 'jigoshop');
	$message = wordwrap(html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8'), 70);
	wp_mail($jigoshop_options->get_option('jigoshop_email'), $subject, $message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");
}

/**
 * Backorder notification emails
 * an email is sent to the admin notifying which product is backordered with an amount needed to fill the order
 * an email -may- be sent to the customer notifying them of the same
 * if sent, an email is sent to the customer for each item backordered in the order
 *
 * @param string $order_id - the System Order number (ID)
 * @param string $product - the Product ID on backorder
 * @param string $amount - the count of the product needed to fill the order
 * */
function jigoshop_product_on_backorder_notification($order_id, $_product, $amount) {
	$jigoshop_options = Jigoshop_Base::get_options();
	$order = new jigoshop_order($order_id);

	// notify the admin
	$subject = html_entity_decode('[' . get_bloginfo('name') . '] ' . sprintf(__('Product Backorder on Order %s', 'jigoshop'), $order->get_order_number()), ENT_QUOTES, 'UTF-8');
	$message = sprintf(__("%s units of #%s %s (#%s) are needed to fill Order %s.", 'jigoshop'), abs($amount), $_product->id, $_product->get_title(), $_product->sku, $order->get_order_number());
	$message = wordwrap(html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8'), 70);
	wp_mail($jigoshop_options->get_option('jigoshop_email'), $subject, $message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");

	// notify the customer if required
	if ($_product->meta['backorders'][0] == 'notify') :

		$subject = html_entity_decode('[' . get_bloginfo('name') . '] ' . sprintf(__('Product Backorder on Order %s', 'jigoshop'), $order->get_order_number()), ENT_QUOTES, 'UTF-8');

		ob_start();
		echo sprintf(__("Thank you for your Order %s. Unfortunately, the following item was found to be on backorder.", 'jigoshop'), $order->get_order_number()) . PHP_EOL . PHP_EOL;

		add_header_info($order);

		echo sprintf(__("%d units of #%d %s (#%s) have been backordered.", 'jigoshop'), abs($amount), $_product->id, $_product->get_title(), $_product->sku);

		echo PHP_EOL . PHP_EOL;
		if ($order->customer_note) :
			echo PHP_EOL . __('Note:', 'jigoshop') . $order->customer_note . PHP_EOL;
		endif;

		do_action('jigoshop_after_email_order_info', $order->id);

		add_customer_details($order);

		add_billing_address_details($order);

		add_shipping_address_details($order);

		$message = ob_get_clean();
		$message = html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8');

		wp_mail($order->billing_email, $subject, $message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");
	endif;
}
Back to Top