class-wc-email-new-order.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * Class WC_Email_New_Order file
  4. *
  5. * @package WooCommerce\Emails
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit;
  9. }
  10. if ( ! class_exists( 'WC_Email_New_Order' ) ) :
  11. /**
  12. * New Order Email.
  13. *
  14. * An email sent to the admin when a new order is received/paid for.
  15. *
  16. * @class WC_Email_New_Order
  17. * @version 2.0.0
  18. * @package WooCommerce/Classes/Emails
  19. * @extends WC_Email
  20. */
  21. class WC_Email_New_Order extends WC_Email {
  22. /**
  23. * Constructor.
  24. */
  25. public function __construct() {
  26. $this->id = 'new_order';
  27. $this->title = __( 'New order', 'woocommerce' );
  28. $this->description = __( 'New order emails are sent to chosen recipient(s) when a new order is received.', 'woocommerce' );
  29. $this->template_html = 'emails/admin-new-order.php';
  30. $this->template_plain = 'emails/plain/admin-new-order.php';
  31. $this->placeholders = array(
  32. '{site_title}' => $this->get_blogname(),
  33. '{order_date}' => '',
  34. '{order_number}' => '',
  35. );
  36. // Triggers for this email.
  37. add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
  38. add_action( 'woocommerce_order_status_pending_to_completed_notification', array( $this, 'trigger' ), 10, 2 );
  39. add_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );
  40. add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
  41. add_action( 'woocommerce_order_status_failed_to_completed_notification', array( $this, 'trigger' ), 10, 2 );
  42. add_action( 'woocommerce_order_status_failed_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );
  43. // Call parent constructor.
  44. parent::__construct();
  45. // Other settings.
  46. $this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ) );
  47. }
  48. /**
  49. * Get email subject.
  50. *
  51. * @since 3.1.0
  52. * @return string
  53. */
  54. public function get_default_subject() {
  55. return __( '[{site_title}] New customer order ({order_number}) - {order_date}', 'woocommerce' );
  56. }
  57. /**
  58. * Get email heading.
  59. *
  60. * @since 3.1.0
  61. * @return string
  62. */
  63. public function get_default_heading() {
  64. return __( 'New customer order', 'woocommerce' );
  65. }
  66. /**
  67. * Trigger the sending of this email.
  68. *
  69. * @param int $order_id The order ID.
  70. * @param WC_Order|false $order Order object.
  71. */
  72. public function trigger( $order_id, $order = false ) {
  73. $this->setup_locale();
  74. if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
  75. $order = wc_get_order( $order_id );
  76. }
  77. if ( is_a( $order, 'WC_Order' ) ) {
  78. $this->object = $order;
  79. $this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
  80. $this->placeholders['{order_number}'] = $this->object->get_order_number();
  81. }
  82. if ( $this->is_enabled() && $this->get_recipient() ) {
  83. $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
  84. }
  85. $this->restore_locale();
  86. }
  87. /**
  88. * Get content html.
  89. *
  90. * @access public
  91. * @return string
  92. */
  93. public function get_content_html() {
  94. return wc_get_template_html(
  95. $this->template_html, array(
  96. 'order' => $this->object,
  97. 'email_heading' => $this->get_heading(),
  98. 'sent_to_admin' => true,
  99. 'plain_text' => false,
  100. 'email' => $this,
  101. )
  102. );
  103. }
  104. /**
  105. * Get content plain.
  106. *
  107. * @access public
  108. * @return string
  109. */
  110. public function get_content_plain() {
  111. return wc_get_template_html(
  112. $this->template_plain, array(
  113. 'order' => $this->object,
  114. 'email_heading' => $this->get_heading(),
  115. 'sent_to_admin' => true,
  116. 'plain_text' => true,
  117. 'email' => $this,
  118. )
  119. );
  120. }
  121. /**
  122. * Initialise settings form fields.
  123. */
  124. public function init_form_fields() {
  125. $this->form_fields = array(
  126. 'enabled' => array(
  127. 'title' => __( 'Enable/Disable', 'woocommerce' ),
  128. 'type' => 'checkbox',
  129. 'label' => __( 'Enable this email notification', 'woocommerce' ),
  130. 'default' => 'yes',
  131. ),
  132. 'recipient' => array(
  133. 'title' => __( 'Recipient(s)', 'woocommerce' ),
  134. 'type' => 'text',
  135. /* translators: %s: WP admin email */
  136. 'description' => sprintf( __( 'Enter recipients (comma separated) for this email. Defaults to %s.', 'woocommerce' ), '<code>' . esc_attr( get_option( 'admin_email' ) ) . '</code>' ),
  137. 'placeholder' => '',
  138. 'default' => '',
  139. 'desc_tip' => true,
  140. ),
  141. 'subject' => array(
  142. 'title' => __( 'Subject', 'woocommerce' ),
  143. 'type' => 'text',
  144. 'desc_tip' => true,
  145. /* translators: %s: list of placeholders */
  146. 'description' => sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>{site_title}, {order_date}, {order_number}</code>' ),
  147. 'placeholder' => $this->get_default_subject(),
  148. 'default' => '',
  149. ),
  150. 'heading' => array(
  151. 'title' => __( 'Email heading', 'woocommerce' ),
  152. 'type' => 'text',
  153. 'desc_tip' => true,
  154. /* translators: %s: list of placeholders */
  155. 'description' => sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>{site_title}, {order_date}, {order_number}</code>' ),
  156. 'placeholder' => $this->get_default_heading(),
  157. 'default' => '',
  158. ),
  159. 'email_type' => array(
  160. 'title' => __( 'Email type', 'woocommerce' ),
  161. 'type' => 'select',
  162. 'description' => __( 'Choose which format of email to send.', 'woocommerce' ),
  163. 'default' => 'html',
  164. 'class' => 'email_type wc-enhanced-select',
  165. 'options' => $this->get_email_type_options(),
  166. 'desc_tip' => true,
  167. ),
  168. );
  169. }
  170. }
  171. endif;
  172. return new WC_Email_New_Order();