Skip to content

Instantly share code, notes, and snippets.

@peterhartree
Created February 7, 2014 18:52
Show Gist options
  • Save peterhartree/8869195 to your computer and use it in GitHub Desktop.
Save peterhartree/8869195 to your computer and use it in GitHub Desktop.
/**
* @extends \WC_Email
*/
class WC_Custom_Order_Email extends WC_Email {
/**
* Set email defaults
*/
public function __construct() {
// set ID, this simply needs to be a unique name
$this->id = 'wc_custom_email';
// title for this email's setting panel in WooCommerce Email settings
$this->title = 'Custom Email';
// email description in WooCommerce email settings
$this->description = 'These order notification emails are sent when a customer places an order.';
// default heading and subject lines (can be overridden using the settings panel)
$this->heading = 'New Order';
$this->subject = 'New Order';
// define the locations of the templates that this email should use
$this->template_html = 'emails/admin-new-order.php';
$this->template_plain = 'emails/plain/admin-new-order.php';
// trigger this email when a new order is placed
// if you want a different behaviour, look for different triggers.
add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ) );
add_action( 'woocommerce_order_status_pending_to_completed_notification', array( $this, 'trigger' ) );
add_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $this, 'trigger' ) );
add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ) );
add_action( 'woocommerce_order_status_failed_to_completed_notification', array( $this, 'trigger' ) );
add_action( 'woocommerce_order_status_failed_to_on-hold_notification', array( $this, 'trigger' ) );
// Call parent constructor to load any other defaults not explicity defined here
parent::__construct();
// this sets the recipient to the settings defined below in init_form_fields()
$this->recipient = $this->get_option( 'recipient' );
// if none was entered, just use the WP admin email as a fallback
if ( ! $this->recipient )
$this->recipient = get_option( 'admin_email' );
}
/**
* Determine if the email should actually be sent and setup email merge variables
*
* @param int $order_id
*/
public function trigger( $order_id ) {
// bail if no order ID is present
if ( ! $order_id )
return;
// setup order object
$this->object = new WC_Order( $order_id );
if ( ! $this->is_enabled() || ! $this->get_recipient() )
return;
// woohoo, send the email!
$this->customSend( $this->get_recipient(), $this->object->billing_first_name, $this->object->billing_email, $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}
/**
* Send the email via a custom sending function that grants more control over header values.
*
* @access public
* @param mixed $to
* @param mixed $subject
* @param mixed $message
* @param string $headers
* @param string $attachments
* @param string $content_type
* @return void
*/
public function customSend( $to, $from_name, $from_address, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = "", $content_type = 'text/html' ) {
// Set content type
$this->_content_type = $content_type;
// Filters for the email
// Note custom method calls that grab customer billing name and email address
add_filter( 'wp_mail_from', array( $this, 'custom_get_from_address' ) );
add_filter( 'wp_mail_from_name', array( $this, 'custom_get_from_name' ) );
add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
// Send
wp_mail( $to, $subject, $message, $headers, $attachments );
// Unhook filters
remove_filter( 'wp_mail_from', array( $this, 'custom_get_from_address' ) );
remove_filter( 'wp_mail_from_name', array( $this, 'custom_get_from_name' ) );
remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ) );
}
/**
* Get customer billing email address
*
* @return string;
*/
public function custom_get_from_address() {
return $this->object->billing_email;
}
/**
* Get customer name
*
* @return string;
*/
public function custom_get_from_name() {
return $this->object->billing_first_name;
}
/**
* get_content_html function.
*
* @return string
*/
public function get_content_html() {
ob_start();
woocommerce_get_template( $this->template_html, array(
'order' => $this->object,
'email_heading' => $this->get_heading()
) );
return ob_get_clean();
}
/**
* get_content_plain function.
*
* @return string
*/
public function get_content_plain() {
ob_start();
woocommerce_get_template( $this->template_plain, array(
'order' => $this->object,
'email_heading' => $this->get_heading()
) );
return ob_get_clean();
}
/**
* Initialize Settings Form Fields
*
*/
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => 'Enable/Disable',
'type' => 'checkbox',
'label' => 'Enable this email notification',
'default' => 'yes'
),
'recipient' => array(
'title' => 'Recipient(s)',
'type' => 'text',
'description' => sprintf( 'Enter recipients (comma separated) for this email. Defaults to <code>%s</code>.', esc_attr( get_option( 'admin_email' ) ) ),
'placeholder' => '',
'default' => ''
),
'subject' => array(
'title' => 'Subject',
'type' => 'text',
'description' => sprintf( 'This controls the email subject line. Leave blank to use the default subject: <code>%s</code>.', $this->subject ),
'placeholder' => '',
'default' => ''
),
'heading' => array(
'title' => 'Email Heading',
'type' => 'text',
'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: <code>%s</code>.' ), $this->heading ),
'placeholder' => '',
'default' => ''
),
'email_type' => array(
'title' => 'Email type',
'type' => 'select',
'description' => 'Choose which format of email to send.',
'default' => 'html',
'class' => 'email_type',
'options' => array(
'plain' => __( 'Plain text', 'woocommerce' ),
'html' => __( 'HTML', 'woocommerce' ),
'multipart' => __( 'Multipart', 'woocommerce' ),
)
)
);
}
} // end \WC_Expedited_Order_Email class
/**
* Add a custom email to the list of emails WooCommerce should load
*
* @param array $email_classes available email classes
* @return array filtered available email classes
*/
function add_custom_order_woocommerce_email( $email_classes ) {
require( 'includes/class-wc-custom-email.php' );
// add the email class to the list of email classes that WooCommerce loads
$email_classes['WC_Custom_Order_Email'] = new WC_Custom_Order_Email();
return $email_classes;
}
add_filter( 'woocommerce_email_classes', 'add_custom_order_woocommerce_email' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment