Bez popisu

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. '{order_date}' => '',
  33. '{order_number}' => '',
  34. );
  35. // Triggers for this email.
  36. add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
  37. add_action( 'woocommerce_order_status_pending_to_completed_notification', array( $this, 'trigger' ), 10, 2 );
  38. add_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );
  39. add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
  40. add_action( 'woocommerce_order_status_failed_to_completed_notification', array( $this, 'trigger' ), 10, 2 );
  41. add_action( 'woocommerce_order_status_failed_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );
  42. add_action( 'woocommerce_order_status_cancelled_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
  43. add_action( 'woocommerce_order_status_cancelled_to_completed_notification', array( $this, 'trigger' ), 10, 2 );
  44. add_action( 'woocommerce_order_status_cancelled_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );
  45. // Call parent constructor.
  46. parent::__construct();
  47. // Other settings.
  48. $this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ) );
  49. }
  50. /**
  51. * Get email subject.
  52. *
  53. * @since 3.1.0
  54. * @return string
  55. */
  56. public function get_default_subject() {
  57. return __( '[{site_title}]: New order #{order_number}', 'woocommerce' );
  58. }
  59. /**
  60. * Get email heading.
  61. *
  62. * @since 3.1.0
  63. * @return string
  64. */
  65. public function get_default_heading() {
  66. return __( 'New Order: #{order_number}', 'woocommerce' );
  67. }
  68. /**
  69. * Trigger the sending of this email.
  70. *
  71. * @param int $order_id The order ID.
  72. * @param WC_Order|false $order Order object.
  73. */
  74. public function trigger( $order_id, $order = false ) {
  75. $this->setup_locale();
  76. if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
  77. $order = wc_get_order( $order_id );
  78. }
  79. if ( is_a( $order, 'WC_Order' ) ) {
  80. $this->object = $order;
  81. $this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
  82. $this->placeholders['{order_number}'] = $this->object->get_order_number();
  83. $email_already_sent = $order->get_meta( '_new_order_email_sent' );
  84. }
  85. /**
  86. * Controls if new order emails can be resend multiple times.
  87. *
  88. * @since 5.0.0
  89. * @param bool $allows Defaults to false.
  90. */
  91. if ( 'true' === $email_already_sent && ! apply_filters( 'woocommerce_new_order_email_allows_resend', false ) ) {
  92. return;
  93. }
  94. if ( $this->is_enabled() && $this->get_recipient() ) {
  95. $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
  96. $order->update_meta_data( '_new_order_email_sent', 'true' );
  97. $order->save();
  98. }
  99. $this->restore_locale();
  100. }
  101. /**
  102. * Get content html.
  103. *
  104. * @return string
  105. */
  106. public function get_content_html() {
  107. return wc_get_template_html(
  108. $this->template_html,
  109. array(
  110. 'order' => $this->object,
  111. 'email_heading' => $this->get_heading(),
  112. 'additional_content' => $this->get_additional_content(),
  113. 'sent_to_admin' => true,
  114. 'plain_text' => false,
  115. 'email' => $this,
  116. )
  117. );
  118. }
  119. /**
  120. * Get content plain.
  121. *
  122. * @return string
  123. */
  124. public function get_content_plain() {
  125. return wc_get_template_html(
  126. $this->template_plain,
  127. array(
  128. 'order' => $this->object,
  129. 'email_heading' => $this->get_heading(),
  130. 'additional_content' => $this->get_additional_content(),
  131. 'sent_to_admin' => true,
  132. 'plain_text' => true,
  133. 'email' => $this,
  134. )
  135. );
  136. }
  137. /**
  138. * Default content to show below main email content.
  139. *
  140. * @since 3.7.0
  141. * @return string
  142. */
  143. public function get_default_additional_content() {
  144. return __( 'Congratulations on the sale.', 'woocommerce' );
  145. }
  146. /**
  147. * Initialise settings form fields.
  148. */
  149. public function init_form_fields() {
  150. /* translators: %s: list of placeholders */
  151. $placeholder_text = sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>' . implode( '</code>, <code>', array_keys( $this->placeholders ) ) . '</code>' );
  152. $this->form_fields = array(
  153. 'enabled' => array(
  154. 'title' => __( 'Enable/Disable', 'woocommerce' ),
  155. 'type' => 'checkbox',
  156. 'label' => __( 'Enable this email notification', 'woocommerce' ),
  157. 'default' => 'yes',
  158. ),
  159. 'recipient' => array(
  160. 'title' => __( 'Recipient(s)', 'woocommerce' ),
  161. 'type' => 'text',
  162. /* translators: %s: WP admin email */
  163. 'description' => sprintf( __( 'Enter recipients (comma separated) for this email. Defaults to %s.', 'woocommerce' ), '<code>' . esc_attr( get_option( 'admin_email' ) ) . '</code>' ),
  164. 'placeholder' => '',
  165. 'default' => '',
  166. 'desc_tip' => true,
  167. ),
  168. 'subject' => array(
  169. 'title' => __( 'Subject', 'woocommerce' ),
  170. 'type' => 'text',
  171. 'desc_tip' => true,
  172. 'description' => $placeholder_text,
  173. 'placeholder' => $this->get_default_subject(),
  174. 'default' => '',
  175. ),
  176. 'heading' => array(
  177. 'title' => __( 'Email heading', 'woocommerce' ),
  178. 'type' => 'text',
  179. 'desc_tip' => true,
  180. 'description' => $placeholder_text,
  181. 'placeholder' => $this->get_default_heading(),
  182. 'default' => '',
  183. ),
  184. 'additional_content' => array(
  185. 'title' => __( 'Additional content', 'woocommerce' ),
  186. 'description' => __( 'Text to appear below the main email content.', 'woocommerce' ) . ' ' . $placeholder_text,
  187. 'css' => 'width:400px; height: 75px;',
  188. 'placeholder' => __( 'N/A', 'woocommerce' ),
  189. 'type' => 'textarea',
  190. 'default' => $this->get_default_additional_content(),
  191. 'desc_tip' => true,
  192. ),
  193. 'email_type' => array(
  194. 'title' => __( 'Email type', 'woocommerce' ),
  195. 'type' => 'select',
  196. 'description' => __( 'Choose which format of email to send.', 'woocommerce' ),
  197. 'default' => 'html',
  198. 'class' => 'email_type wc-enhanced-select',
  199. 'options' => $this->get_email_type_options(),
  200. 'desc_tip' => true,
  201. ),
  202. );
  203. }
  204. }
  205. endif;
  206. return new WC_Email_New_Order();