Home / Admin / Supports sending an email with a Stripe Pre-Approved payment is submitted.
Duplicate Snippet

Embed Snippet on Your Site

Supports sending an email with a Stripe Pre-Approved payment is submitted.

By default EDD only sends a purchase confirmation when an order is marked as completed, however when using the EDD Stripe PreApproval setting, an email is not sent by default (this will be fixed in a future version of EDD). For now you can use this snippet to send an email when an order transitions to the 'preapproval' status.

This requires EDD 3.1.2+

It does not register any settings, so all email content needs to be directly edited in the snippet.

Code Preview
php
<?php
add_action( 'init', function() {
	if ( ! class_exists( '\EDD\Emails\Types\Email' ) ) {
		return;
	}
	/**
	 * The PreApproved email class.
	 *
	 * This is sent to customers as their 'preapproved payment confirmation'.
	 */
	class EDDWP_PreApproved_Email extends \EDD\Emails\Types\Email {
		/**
		 * The email ID.
		 *
		 * @var string
		 */
		protected $id = 'preapproved';
		/**
		 * The email context.
		 *
		 * @var string
		 */
		protected $context = 'order';
		/**
		 * The email recipient type.
		 *
		 * @var string
		 */
		protected $recipient_type = 'customer';
		/**
		 * The order object.
		 *
		 * @var EDD\Orders\Order
		 */
		protected $order;
		/**
		 * The order ID.
		 *
		 * @var int
		 */
		protected $order_id;
		/**
		 * OrderReceipt constructor.
		 *
		 * @param EDD\Orders\Order $order The order object.
		 * @return void
		 */
		public function __construct( $order ) {
			$this->order = $order;
			// To support previews, we need to possibly set a `0` order ID.
			$this->order_id = false !== $order ? $order->id : 0;
		}
		/**
		 * Get the order of the emails.
		 *
		 * @return EDD\Orders\Order The order object.
		 */
		public function get_order() {
			return $this->order;
		}
		/**
		 * Set the raw email body content.
		 *
		 * This will add the email content to the `raw_body_content` property. It has not yet had
		 * the tag replacements executed.
		 *
		 * @return void
		 */
		protected function set_email_body_content() {
			$this->raw_body_content = $this->get_default_body_content();
		}
		/**
		 * Set the 'from' name on the email.
		 *
		 * @return void
		 */
		protected function set_from_name() {
			$this->from_name = edd_get_option( 'from_name', wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) );
		}
		/**
		 * Set the 'from' email on the email.
		 *
		 * @return void
		 */
		protected function set_from_email() {
			$this->from_email = edd_get_option( 'from_email', get_bloginfo( 'admin_email' ) );
		}
		/**
		 * Set the 'to' email on the email.
		 *
		 * @return void
		 */
		protected function set_to_email() {
			if ( empty( $this->send_to ) && ! empty( $this->order->email ) ) {
				$this->send_to = $this->order->email;
			}
		}
		/**
		 * Set the headers email on the email.
		 *
		 * @return void
		 */
		protected function set_headers() {
			$this->headers = $this->processor()->get_headers();
		}
		/**
		 * Set the subject on the email.
		 *
		 * @return void
		 */
		protected function set_subject() {
			$this->subject = __( 'Thank you for your Pre-Sale Order', 'easy-digital-downloads' );
			$this->subject = wp_strip_all_tags( $this->process_tags( $this->subject, $this->order_id ) );
		}
		/**
		 * Set the heading on the email.
		 *
		 * @return void
		 */
		protected function set_heading() {
			$this->heading = __( 'Preapproved Payment Confirmation', 'easy-digital-downloads' );
			$this->heading = $this->process_tags( $this->heading, $this->order_id );
		}
		/**
		 * Set the message on the email.
		 *
		 * @return void
		 */
		protected function set_message() {
			$message = $this->get_raw_body_content();
			$this->message = $this->process_tags( $this->maybe_apply_autop( $message ), $this->order_id );
		}
		/**
		 * Set the attachments on the email.
		 *
		 * @return void
		 */
		protected function set_attachments() {
			$this->attachments = array();
		}
		/**
		 * Get the default email body content.
		 *
		 * @return string
		 */
		public function get_default_body_content() {
			$default_email_body  = __( 'Dear', 'easy-digital-downloads' ) . " {name},\n\n";
			$default_email_body .= __( 'Your pre-order has been received. Your payment method has not been charged yet, however you may see a pending authorization on your statement. You payment will be charged once the pre-order period is over.', 'easy-digital-downloads' ) . "\n\n";
			$default_email_body .= 'Pre-Sale Price: {price}' . "\n\n";
			$default_email_body .= '{sitename}';
			return $default_email_body;
		}
		/**
		 * Allows filtering to disable sending the default order receipt email.
		 *
		 * @return bool
		 */
		protected function should_send() {
			// Allow developers to unhook this email via a filter.
			if ( true === apply_filters( 'edd_disable_' . $this->id, false, $this ) ) {
				return false;
			}
			return true;
		}
	}
	// Register the email for preapproved payments.
	EDD\Emails\Registry::register( 'preapproved', 'EDDWP_PreApproved_Email' );
} );
add_action( 'edd_transition_order_status', function( $old_status, $new_status, $order_id  ) {
	// If the Email base class doens't exist, bail.
	if ( ! class_exists( '\EDD\Emails\Types\Email' ) ) {
		return;
	}
	// If the order is not a preapproved payment, bail.
	edd_debug_log( 'transition_order_status: ' . $old_status . ' -> ' . $new_status );
	if ( 'preapproval' !== $new_status ) {
		return;
	}
	$order = edd_get_order( $order_id );
	// If the order is transitioning to complete, send the preapproved payment email.
	$email = new EDDWP_PreApproved_Email( $order );
	$email->send();
}, 99, 3 );

Comments

Add a Comment