Skip to content

Instantly share code, notes, and snippets.

@bUxEE
Last active September 24, 2018 09:39
Show Gist options
  • Save bUxEE/0b44f512eb11e71bbcb74c640bfe2080 to your computer and use it in GitHub Desktop.
Save bUxEE/0b44f512eb11e71bbcb74c640bfe2080 to your computer and use it in GitHub Desktop.
/**
* Extra emails wc
*/
class wopeckoStatusEmails {
function __construct($id,$title,$description) {
$this->ID = $id;
$this->title = $title;
$this->description = $description;
add_filter( 'woocommerce_email_classes', array($this, 'add_custom_order_status_woocommerce_email') );
add_action( 'woocommerce_order_status_changed', array( $this, 'conditional_trigger' ), 99, 4 );
}
function add_custom_order_status_woocommerce_email( $email_classes ) {
if ( ! isset( $email_classes[ 'wopeckoBookedOrderEmail' ] ) ) {
require_once( 'subclasses/woo-extra-emails.php' );
// add the email class to the list of email classes that WooCommerce loads
$email_classes['wopeckoBookedOrderEmail'] = new wopeckoCustomStatusEmail($this->ID, $this->title, $this->description);
}
return $email_classes;
}
public function conditional_trigger($order_id, $from, $to, $order) {
if($to === $this->ID) {
new WC_Emails();
do_action( 'wopecko_'.$this->ID.'_status_trigger', $order_id );
}
return;
}
}
$wopeckoBookedOrderEmail = new wopeckoStatusEmails("booked","Booked","This email is sent when the order enters the booked status");
<?php
class wopeckoCustomStatusEmail extends WC_Email
{
public function __construct($id,$title,$description) {
$this->setID = $id;
$this->setTitle = $title;
// set ID, this simply needs to be a unique name
$this->id = 'wc_'.$id.'_order';
// this is the title in WooCommerce Email settings
$this->title = $title.' Order';
// this is the description in WooCommerce email settings
$this->description = $description;
// these are the default heading and subject lines that can be overridden using the settings
$this->heading = $this->title.' status';
$this->subject = $this->title.' status';
// these define the locations of the templates that this email should use, we'll just use the new order template since this email is similar
$this->template_html = 'emails/customer-'.$id.'-order.php';
$this->template_plain = 'emails/plain/customer-'.$id.'-order.php';
// 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' );
// trigger
add_action( 'wopecko_'.$this->setID.'_status_trigger', array($this, 'trigger') );
}
/**
* Determine if the email should actually be sent and setup email merge variables
*
* @since 0.1
* @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->recipient )
$this->recipient = $this->object->get_billing_email();
// bail if shipping method is not booked
if ( $this->object->get_status() !== $this->setID )
return;
// replace variables in the subject/headings
$this->find[] = '{order_date}';
$this->replace[] = date_i18n( woocommerce_date_format(), strtotime( $this->object->order_date ) );
$this->find[] = '{order_number}';
$this->replace[] = $this->object->get_order_number();
if ( ! $this->is_enabled() || ! $this->get_recipient() )
return;
// woohoo, send the email!
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}
/**
* get_content_html function.
*
* @since 0.1
* @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.
*
* @since 0.1
* @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
*
* @since 2.0
*/
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' ),
)
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment