PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/jigoshop_emails.php

https://github.com/scottpoulin/jigoshop
PHP | 515 lines | 424 code | 49 blank | 42 comment | 5 complexity | a3fa41d661ce8e51f82d2b5d04464a92 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /**
  3. * Jigoshop Emails
  4. *
  5. * DISCLAIMER
  6. *
  7. * Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
  8. * versions in the future. If you wish to customise Jigoshop core for your needs,
  9. * please use our GitHub repository to publish essential changes for consideration.
  10. *
  11. * @package Jigoshop
  12. * @category Core
  13. * @author Jigoshop
  14. * @copyright Copyright Š 2011-2013 Jigoshop.
  15. * @license http://jigoshop.com/license/commercial-edition
  16. */
  17. /**
  18. * Hooks for emails
  19. * */
  20. add_action('jigoshop_low_stock_notification', 'jigoshop_low_stock_notification');
  21. add_action('jigoshop_no_stock_notification', 'jigoshop_no_stock_notification');
  22. add_action('jigoshop_product_on_backorder_notification', 'jigoshop_product_on_backorder_notification', 1, 3);
  23. /**
  24. * New order notification email template
  25. * */
  26. add_action('order_status_pending_to_processing', 'jigoshop_new_order_notification');
  27. add_action('order_status_pending_to_completed', 'jigoshop_new_order_notification');
  28. add_action('order_status_pending_to_on-hold', 'jigoshop_new_order_notification');
  29. function jigoshop_new_order_notification($order_id) {
  30. $jigoshop_options = Jigoshop_Base::get_options();
  31. $order = new jigoshop_order($order_id);
  32. $subject = html_entity_decode(sprintf(__('[%s] New Customer Order (%s)', 'jigoshop'), get_bloginfo('name'), $order->get_order_number()), ENT_QUOTES, 'UTF-8');
  33. ob_start();
  34. 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;
  35. add_header_info($order);
  36. add_order_totals($order, false, true);
  37. add_customer_details($order);
  38. add_billing_address_details($order);
  39. add_shipping_address_details($order);
  40. $message = ob_get_clean();
  41. $message = apply_filters('jigoshop_change_new_order_email_contents', $message, $order);
  42. $message = html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8');
  43. wp_mail($jigoshop_options->get_option('jigoshop_email'), $subject, $message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");
  44. }
  45. /**
  46. * Processing order notification email template
  47. * */
  48. add_action('order_status_pending_to_processing', 'jigoshop_processing_order_customer_notification');
  49. add_action('order_status_pending_to_on-hold', 'jigoshop_processing_order_customer_notification');
  50. function jigoshop_processing_order_customer_notification($order_id) {
  51. $jigoshop_options = Jigoshop_Base::get_options();
  52. $order = new jigoshop_order($order_id);
  53. $subject = html_entity_decode('[' . get_bloginfo('name') . '] ' . __('Order Received', 'jigoshop'), ENT_QUOTES, 'UTF-8');
  54. ob_start();
  55. echo __("Thank you, we are now processing your order. Your order's details are below:", 'jigoshop') . PHP_EOL . PHP_EOL;
  56. add_header_info($order);
  57. add_order_totals($order, false, true);
  58. if (strtolower($order->payment_method) == "bank_transfer") :
  59. echo add_email_separator( '-' ) . PHP_EOL;
  60. echo __('BANK PAYMENT DETAILS', 'jigoshop') . PHP_EOL;
  61. echo add_email_separator( '-' ) . PHP_EOL;
  62. echo jigoshop_bank_transfer::get_bank_details();
  63. echo PHP_EOL;
  64. do_action('jigoshop_after_email_bank_payment_details', $order->id);
  65. endif;
  66. add_customer_details($order);
  67. add_billing_address_details($order);
  68. add_shipping_address_details($order);
  69. $message = ob_get_clean();
  70. $message = apply_filters('jigoshop_change_processing_order_email_contents', $message, $order);
  71. $message = html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8');
  72. wp_mail($order->billing_email, $subject, $message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");
  73. }
  74. /**
  75. * Completed order notification email template - this one includes download links for downloadable products
  76. * */
  77. add_action('order_status_completed', 'jigoshop_completed_order_customer_notification');
  78. function jigoshop_completed_order_customer_notification($order_id) {
  79. $jigoshop_options = Jigoshop_Base::get_options();
  80. $order = new jigoshop_order($order_id);
  81. $subject = html_entity_decode('[' . get_bloginfo('name') . '] ' . __('Order Complete', 'jigoshop'), ENT_QUOTES, 'UTF-8');
  82. ob_start();
  83. echo __("Your order is complete. Your order's details are below:", 'jigoshop') . PHP_EOL . PHP_EOL;
  84. add_header_info($order);
  85. $download_links = apply_filters('jigoshop_download_links_on_completed',true);
  86. add_order_totals($order, true, true);
  87. add_customer_details($order);
  88. add_billing_address_details($order);
  89. add_shipping_address_details($order);
  90. $message = ob_get_clean();
  91. $message = apply_filters('jigoshop_change_completed_order_email_contents', $message, $order);
  92. $message = html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8');
  93. $message = apply_filters('jigoshop_completed_order_customer_notification_mail_message', $message);
  94. wp_mail($order->billing_email, $subject, $message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");
  95. }
  96. /**
  97. * Refunded order notification email template - this one does not include download links for downloadable products
  98. * */
  99. add_action('order_status_refunded', 'jigoshop_refunded_order_customer_notification');
  100. function jigoshop_refunded_order_customer_notification($order_id) {
  101. $jigoshop_options = Jigoshop_Base::get_options();
  102. $order = new jigoshop_order($order_id);
  103. $subject = html_entity_decode('[' . get_bloginfo('name') . '] ' . __('Order Refunded', 'jigoshop'), ENT_QUOTES, 'UTF-8');
  104. ob_start();
  105. echo __("Your order has been refunded. Your order's details are below:", 'jigoshop') . PHP_EOL . PHP_EOL;
  106. add_header_info($order);
  107. add_order_totals($order, false, true);
  108. add_customer_details($order);
  109. add_billing_address_details($order);
  110. add_shipping_address_details($order);
  111. $message = ob_get_clean();
  112. $message = apply_filters('jigoshop_change_refunded_email_message', $message, $order);
  113. $message = html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8');
  114. $message = apply_filters('jigoshop_refunded_order_customer_notification_mail_message', $message);
  115. wp_mail($order->billing_email, $subject, $message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");
  116. }
  117. /**
  118. * Customer invoice for an order.
  119. *
  120. * Displays link for payment if the order is marked pending.
  121. * Includes download link if order is completed.
  122. * */
  123. function jigoshop_send_customer_invoice($order_id) {
  124. $jigoshop_options = Jigoshop_Base::get_options();
  125. $order = new jigoshop_order($order_id);
  126. $subject = html_entity_decode('[' . get_bloginfo('name') . '] ' . sprintf(__('Invoice for Order %s', 'jigoshop'), $order->get_order_number()), ENT_QUOTES, 'UTF-8');
  127. $customer_message = '';
  128. if ($order->status == 'pending') :
  129. $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());
  130. endif;
  131. ob_start();
  132. add_header_info($order);
  133. if ($order->status == 'completed') :
  134. $download_links = apply_filters('jigoshop_download_links_on_invoice',true);
  135. add_order_totals($order, true, true);
  136. else :
  137. add_order_totals($order, false, true);
  138. endif;
  139. $message = ob_get_clean();
  140. $message = apply_filters('jigoshop_change_pay_order_email_contents', $message, $order);
  141. $customer_message = html_entity_decode(strip_tags($customer_message . $message), ENT_QUOTES, 'UTF-8');
  142. wp_mail($order->billing_email, $subject, $customer_message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");
  143. }
  144. function add_header_info($order) {
  145. echo add_email_separator( '=' ) . PHP_EOL;
  146. add_company_information();
  147. $info = __('ORDER ', 'jigoshop') . $order->get_order_number();
  148. $date = __('Date: ','jigoshop') . date_i18n( get_option('date_format') );
  149. $info .= add_padding_to_email_lines( 80 - strlen( $date ) - strlen( $info ) );
  150. $info .= $date;
  151. echo $info . PHP_EOL;
  152. echo add_email_separator( '=' ) . PHP_EOL;
  153. }
  154. function add_email_separator( $char ) {
  155. $sep = '';
  156. for ( $i = 0 ; $i < 80 ; $i++ ) {
  157. $sep .= $char;
  158. }
  159. return $sep;
  160. }
  161. function add_padding_to_email_lines( $amount ) {
  162. $padding = '';
  163. for ( $i = 0 ; $i < $amount ; $i++ ) {
  164. $padding .= ' ';
  165. }
  166. return $padding;
  167. }
  168. function add_company_information() {
  169. $jigoshop_options = Jigoshop_Base::get_options();
  170. $add_eol = false;
  171. if ($jigoshop_options->get_option('jigoshop_company_name')) :
  172. echo $jigoshop_options->get_option('jigoshop_company_name') . PHP_EOL;
  173. $add_eol = true;
  174. endif;
  175. if ($jigoshop_options->get_option('jigoshop_address_line1')) :
  176. $add_eol = true;
  177. echo $jigoshop_options->get_option('jigoshop_address_line1') . PHP_EOL;
  178. if ($jigoshop_options->get_option('jigoshop_address_line2')) :
  179. echo $jigoshop_options->get_option('jigoshop_address_line2') . PHP_EOL;
  180. endif;
  181. endif;
  182. if ($jigoshop_options->get_option('jigoshop_company_phone')) :
  183. $add_eol = true;
  184. echo $jigoshop_options->get_option('jigoshop_company_phone') . PHP_EOL;
  185. endif;
  186. if ($jigoshop_options->get_option('jigoshop_company_email')) :
  187. $add_eol = true;
  188. echo '<a href="mailto:' . $jigoshop_options->get_option('jigoshop_company_email') . '">' . $jigoshop_options->get_option('jigoshop_company_email') . '</a>' . PHP_EOL;
  189. endif;
  190. if ($add_eol) echo PHP_EOL;
  191. }
  192. function add_order_totals($order, $show_download_links, $show_sku) {
  193. do_action('jigoshop_before_email_order_info', $order->id);
  194. $jigoshop_options = Jigoshop_Base::get_options();
  195. $inc_tax = ($jigoshop_options->get_option('jigoshop_calc_taxes') == 'no')||($jigoshop_options->get_option('jigoshop_prices_include_tax') == 'yes');
  196. echo PHP_EOL;
  197. echo $order->email_order_items_list($show_download_links, $show_sku, $inc_tax);
  198. if ( $order->customer_note ) {
  199. echo PHP_EOL . __('Note:', 'jigoshop') . $order->customer_note . PHP_EOL;
  200. }
  201. if ( ( $jigoshop_options->get_option('jigoshop_calc_taxes') == 'yes' && $order->has_compound_tax() )
  202. || ( $jigoshop_options->get_option('jigoshop_tax_after_coupon') == 'yes' && $order->order_discount > 0) ) {
  203. echo PHP_EOL;
  204. $info = __('Retail Price:', 'jigoshop');
  205. $info .= add_padding_to_email_lines( 30 - strlen( $info ) );
  206. $info .= html_entity_decode($order->get_subtotal_to_display(), ENT_QUOTES, 'UTF-8');
  207. echo $info . PHP_EOL;
  208. } else {
  209. echo PHP_EOL;
  210. $info = __('Subtotal:', 'jigoshop');
  211. $info .= add_padding_to_email_lines( 30 - strlen( $info ) );
  212. $info .= html_entity_decode($order->get_subtotal_to_display(), ENT_QUOTES, 'UTF-8');
  213. echo $info . PHP_EOL;
  214. }
  215. if ( $order->order_shipping > 0 ) {
  216. $info = __('Shipping:', 'jigoshop');
  217. $info .= add_padding_to_email_lines( 30 - strlen( $info ) );
  218. $info .= html_entity_decode($order->get_shipping_to_display(), ENT_QUOTES, 'UTF-8');
  219. echo $info . PHP_EOL;
  220. }
  221. do_action('jigoshop_email_order_professing_fee_info', $order->id);
  222. if ( $jigoshop_options->get_option('jigoshop_tax_after_coupon') == 'yes' && $order->order_discount > 0 ) {
  223. $info = __('Discount:', 'jigoshop');
  224. $info .= add_padding_to_email_lines( 30 - strlen( $info ) );
  225. $info .= html_entity_decode(jigoshop_price($order->order_discount), ENT_QUOTES, 'UTF-8');
  226. echo $info . PHP_EOL;
  227. }
  228. // if ( ($jigoshop_options->get_option('jigoshop_calc_taxes') == 'yes' && $order->has_compound_tax())
  229. // || ($jigoshop_options->get_option('jigoshop_tax_after_coupon') == 'yes' && $order->order_discount > 0)) {
  230. //
  231. // $info = __('Subtotal:', 'jigoshop');
  232. // $info .= add_padding_to_email_lines( 30 - strlen( $info ) );
  233. // $info .= html_entity_decode(jigoshop_price($order->order_discount_subtotal), ENT_QUOTES, 'UTF-8');
  234. // echo $info . PHP_EOL;
  235. //
  236. // }
  237. if ( $jigoshop_options->get_option('jigoshop_calc_taxes') == 'yes') {
  238. foreach ($order->get_tax_classes() as $tax_class) {
  239. if ($order->show_tax_entry($tax_class)) {
  240. $info = $order->get_tax_class_for_display($tax_class) . ' (' . (float) $order->get_tax_rate($tax_class) . '%):';
  241. $info .= add_padding_to_email_lines( 30 - strlen( $info ) );
  242. $info .= html_entity_decode($order->get_tax_amount($tax_class), ENT_QUOTES, 'UTF-8');
  243. echo $info . PHP_EOL;
  244. }
  245. }
  246. }
  247. if ( $jigoshop_options->get_option('jigoshop_tax_after_coupon') == 'no' && $order->order_discount > 0 ) {
  248. $info = __('Discount:', 'jigoshop');
  249. $info .= add_padding_to_email_lines( 30 - strlen( $info ) );
  250. $info .= html_entity_decode(jigoshop_price($order->order_discount), ENT_QUOTES, 'UTF-8');
  251. echo $info . PHP_EOL;
  252. }
  253. $method = $order->payment_method_title <> '' ? ucwords($order->payment_method_title) : __("Free",'jigoshop');
  254. $info = __('Total:', 'jigoshop');
  255. $info .= add_padding_to_email_lines( 30 - strlen( $info ) );
  256. $info .= html_entity_decode(jigoshop_price($order->order_total), ENT_QUOTES, 'UTF-8');
  257. $info .= ' - ' . __('via', 'jigoshop') . ' ' . $method;
  258. echo $info . PHP_EOL . PHP_EOL;
  259. if ($jigoshop_options->get_option('jigoshop_calc_taxes') && $jigoshop_options->get_option('jigoshop_tax_number')) :
  260. echo $jigoshop_options->get_option('jigoshop_tax_number') . PHP_EOL . PHP_EOL;
  261. endif;
  262. do_action('jigoshop_after_email_order_info', $order->id);
  263. }
  264. function add_customer_details($order) {
  265. echo add_email_separator( '-' ) . PHP_EOL;
  266. echo __('CUSTOMER DETAILS', 'jigoshop') . PHP_EOL;
  267. echo add_email_separator( '-' ) . PHP_EOL;
  268. if ($order->billing_email) {
  269. $temp = __('Email:', 'jigoshop');
  270. echo $temp . add_padding_to_email_lines( 30 - strlen( $temp ) ) . $order->billing_email . PHP_EOL;
  271. }
  272. if ($order->billing_phone) {
  273. $temp = __('Tel:', 'jigoshop');
  274. echo $temp . add_padding_to_email_lines( 30 - strlen( $temp ) ) . $order->billing_phone . PHP_EOL;
  275. }
  276. if ( $order->billing_euvatno ) {
  277. $temp = __('EU VAT Number:', 'jigoshop');
  278. echo $temp . add_padding_to_email_lines( 30 - strlen( $temp ) ) . $order->billing_euvatno . PHP_EOL;
  279. }
  280. echo PHP_EOL;
  281. do_action('jigoshop_after_email_customer_details', $order->id);
  282. }
  283. function add_billing_address_details($order) {
  284. echo add_email_separator( '-' ) . PHP_EOL;
  285. echo __('BILLING ADDRESS', 'jigoshop') . PHP_EOL;
  286. echo add_email_separator( '-' ) . PHP_EOL;
  287. echo $order->billing_first_name . ' ' . $order->billing_last_name . PHP_EOL;
  288. if ($order->billing_company)
  289. echo $order->billing_company . PHP_EOL;
  290. echo $order->formatted_billing_address . PHP_EOL . PHP_EOL;
  291. do_action('jigoshop_after_email_billing_address', $order->id);
  292. }
  293. function add_shipping_address_details($order) {
  294. echo add_email_separator( '-' ) . PHP_EOL;
  295. echo __('SHIPPING ADDRESS', 'jigoshop') . PHP_EOL;
  296. echo add_email_separator( '-' ) . PHP_EOL;
  297. if ( $order->shipping_method != 'local_pickup' ) {
  298. echo $order->shipping_first_name . ' ' . $order->shipping_last_name . PHP_EOL;
  299. if ($order->shipping_company) echo $order->shipping_company . PHP_EOL;
  300. echo $order->formatted_shipping_address . PHP_EOL . PHP_EOL;
  301. echo __('Shipping: ','jigoshop') . html_entity_decode(ucwords($order->shipping_service), ENT_QUOTES, 'UTF-8') . PHP_EOL . PHP_EOL;
  302. do_action('jigoshop_after_email_shipping_address', $order->id);
  303. } else {
  304. echo __('To be picked up by:', 'jigoshop') . PHP_EOL;
  305. echo $order->shipping_first_name . ' ' . $order->shipping_last_name . PHP_EOL;
  306. if ($order->shipping_company) echo $order->shipping_company . PHP_EOL;
  307. echo PHP_EOL;
  308. echo __('At location:', 'jigoshop') . PHP_EOL;
  309. echo add_company_information() . PHP_EOL . PHP_EOL;
  310. }
  311. }
  312. /**
  313. * Low stock notification email
  314. * */
  315. function jigoshop_low_stock_notification($_product) {
  316. $jigoshop_options = Jigoshop_Base::get_options();
  317. $subject = html_entity_decode('[' . get_bloginfo('name') . '] ' . __('Product low in stock', 'jigoshop'), ENT_QUOTES, 'UTF-8');
  318. $message = '#' . $_product->id . ' ' . $_product->get_title() . ' (' . $_product->sku . ') ' . __('is low in stock.', 'jigoshop');
  319. $message = wordwrap(html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8'), 70);
  320. wp_mail($jigoshop_options->get_option('jigoshop_email'), $subject, $message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");
  321. }
  322. /**
  323. * No stock notification email
  324. * */
  325. function jigoshop_no_stock_notification($_product) {
  326. $jigoshop_options = Jigoshop_Base::get_options();
  327. $subject = html_entity_decode('[' . get_bloginfo('name') . '] ' . __('Product out of stock', 'jigoshop'), ENT_QUOTES, 'UTF-8');
  328. $message = '#' . $_product->id . ' ' . $_product->get_title() . ' (' . $_product->sku . ') ' . __('is out of stock.', 'jigoshop');
  329. $message = wordwrap(html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8'), 70);
  330. wp_mail($jigoshop_options->get_option('jigoshop_email'), $subject, $message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");
  331. }
  332. /**
  333. * Backorder notification emails
  334. * an email is sent to the admin notifying which product is backordered with an amount needed to fill the order
  335. * an email -may- be sent to the customer notifying them of the same
  336. * if sent, an email is sent to the customer for each item backordered in the order
  337. *
  338. * @param string $order_id - the System Order number (ID)
  339. * @param string $product - the Product ID on backorder
  340. * @param string $amount - the count of the product needed to fill the order
  341. * */
  342. function jigoshop_product_on_backorder_notification($order_id, $_product, $amount) {
  343. $jigoshop_options = Jigoshop_Base::get_options();
  344. $order = new jigoshop_order($order_id);
  345. // notify the admin
  346. $subject = html_entity_decode('[' . get_bloginfo('name') . '] ' . sprintf(__('Product Backorder on Order %s', 'jigoshop'), $order->get_order_number()), ENT_QUOTES, 'UTF-8');
  347. $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());
  348. $message = wordwrap(html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8'), 70);
  349. wp_mail($jigoshop_options->get_option('jigoshop_email'), $subject, $message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");
  350. // notify the customer if required
  351. if ($_product->meta['backorders'][0] == 'notify') :
  352. $subject = html_entity_decode('[' . get_bloginfo('name') . '] ' . sprintf(__('Product Backorder on Order %s', 'jigoshop'), $order->get_order_number()), ENT_QUOTES, 'UTF-8');
  353. ob_start();
  354. 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;
  355. add_header_info($order);
  356. echo sprintf(__("%d units of #%d %s (#%s) have been backordered.", 'jigoshop'), abs($amount), $_product->id, $_product->get_title(), $_product->sku);
  357. echo PHP_EOL . PHP_EOL;
  358. if ($order->customer_note) :
  359. echo PHP_EOL . __('Note:', 'jigoshop') . $order->customer_note . PHP_EOL;
  360. endif;
  361. do_action('jigoshop_after_email_order_info', $order->id);
  362. add_customer_details($order);
  363. add_billing_address_details($order);
  364. add_shipping_address_details($order);
  365. $message = ob_get_clean();
  366. $message = html_entity_decode(strip_tags($message), ENT_QUOTES, 'UTF-8');
  367. wp_mail($order->billing_email, $subject, $message, "From: " . $jigoshop_options->get_option('jigoshop_email') . "\r\n");
  368. endif;
  369. }