class-wc-email-failed-order.php 5.4 KB

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