Sin descripción

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 pending 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. '{order_date}' => '',
  33. '{order_number}' => '',
  34. );
  35. // Triggers for this email.
  36. add_action( 'woocommerce_order_status_pending_to_failed_notification', array( $this, 'trigger' ), 10, 2 );
  37. add_action( 'woocommerce_order_status_on-hold_to_failed_notification', array( $this, 'trigger' ), 10, 2 );
  38. // Call parent constructor.
  39. parent::__construct();
  40. // Other settings.
  41. $this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ) );
  42. }
  43. /**
  44. * Get email subject.
  45. *
  46. * @since 3.1.0
  47. * @return string
  48. */
  49. public function get_default_subject() {
  50. return __( '[{site_title}]: Order #{order_number} has failed', 'woocommerce' );
  51. }
  52. /**
  53. * Get email heading.
  54. *
  55. * @since 3.1.0
  56. * @return string
  57. */
  58. public function get_default_heading() {
  59. return __( 'Order Failed: #{order_number}', 'woocommerce' );
  60. }
  61. /**
  62. * Trigger the sending of this email.
  63. *
  64. * @param int $order_id The order ID.
  65. * @param WC_Order|false $order Order object.
  66. */
  67. public function trigger( $order_id, $order = false ) {
  68. $this->setup_locale();
  69. if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
  70. $order = wc_get_order( $order_id );
  71. }
  72. if ( is_a( $order, 'WC_Order' ) ) {
  73. $this->object = $order;
  74. $this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
  75. $this->placeholders['{order_number}'] = $this->object->get_order_number();
  76. }
  77. if ( $this->is_enabled() && $this->get_recipient() ) {
  78. $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
  79. }
  80. $this->restore_locale();
  81. }
  82. /**
  83. * Get content html.
  84. *
  85. * @return string
  86. */
  87. public function get_content_html() {
  88. return wc_get_template_html(
  89. $this->template_html,
  90. array(
  91. 'order' => $this->object,
  92. 'email_heading' => $this->get_heading(),
  93. 'additional_content' => $this->get_additional_content(),
  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,
  108. array(
  109. 'order' => $this->object,
  110. 'email_heading' => $this->get_heading(),
  111. 'additional_content' => $this->get_additional_content(),
  112. 'sent_to_admin' => true,
  113. 'plain_text' => true,
  114. 'email' => $this,
  115. )
  116. );
  117. }
  118. /**
  119. * Default content to show below main email content.
  120. *
  121. * @since 3.7.0
  122. * @return string
  123. */
  124. public function get_default_additional_content() {
  125. return __( 'Hopefully they’ll be back. Read more about <a href="https://docs.woocommerce.com/document/managing-orders/">troubleshooting failed payments</a>.', 'woocommerce' );
  126. }
  127. /**
  128. * Initialise settings form fields.
  129. */
  130. public function init_form_fields() {
  131. /* translators: %s: list of placeholders */
  132. $placeholder_text = sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>' . esc_html( implode( '</code>, <code>', array_keys( $this->placeholders ) ) ) . '</code>' );
  133. $this->form_fields = array(
  134. 'enabled' => array(
  135. 'title' => __( 'Enable/Disable', 'woocommerce' ),
  136. 'type' => 'checkbox',
  137. 'label' => __( 'Enable this email notification', 'woocommerce' ),
  138. 'default' => 'yes',
  139. ),
  140. 'recipient' => array(
  141. 'title' => __( 'Recipient(s)', 'woocommerce' ),
  142. 'type' => 'text',
  143. /* translators: %s: WP admin email */
  144. 'description' => sprintf( __( 'Enter recipients (comma separated) for this email. Defaults to %s.', 'woocommerce' ), '<code>' . esc_attr( get_option( 'admin_email' ) ) . '</code>' ),
  145. 'placeholder' => '',
  146. 'default' => '',
  147. 'desc_tip' => true,
  148. ),
  149. 'subject' => array(
  150. 'title' => __( 'Subject', 'woocommerce' ),
  151. 'type' => 'text',
  152. 'desc_tip' => true,
  153. 'description' => $placeholder_text,
  154. 'placeholder' => $this->get_default_subject(),
  155. 'default' => '',
  156. ),
  157. 'heading' => array(
  158. 'title' => __( 'Email heading', 'woocommerce' ),
  159. 'type' => 'text',
  160. 'desc_tip' => true,
  161. 'description' => $placeholder_text,
  162. 'placeholder' => $this->get_default_heading(),
  163. 'default' => '',
  164. ),
  165. 'additional_content' => array(
  166. 'title' => __( 'Additional content', 'woocommerce' ),
  167. 'description' => __( 'Text to appear below the main email content.', 'woocommerce' ) . ' ' . $placeholder_text,
  168. 'css' => 'width:400px; height: 75px;',
  169. 'placeholder' => __( 'N/A', 'woocommerce' ),
  170. 'type' => 'textarea',
  171. 'default' => $this->get_default_additional_content(),
  172. 'desc_tip' => true,
  173. ),
  174. 'email_type' => array(
  175. 'title' => __( 'Email type', 'woocommerce' ),
  176. 'type' => 'select',
  177. 'description' => __( 'Choose which format of email to send.', 'woocommerce' ),
  178. 'default' => 'html',
  179. 'class' => 'email_type wc-enhanced-select',
  180. 'options' => $this->get_email_type_options(),
  181. 'desc_tip' => true,
  182. ),
  183. );
  184. }
  185. }
  186. endif;
  187. return new WC_Email_Failed_Order();