Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Registering custom emails for WooCommerce
<?php
class CustomOrderEmail extends \WC_Email {
// This is a just a simple example. See \WC_Email and child classes for more examples
public function __construct() {
$this->id = 'custom_order_email';
$this->title = 'Custom Order Email';
$this->description = 'A custom order email that does …';
$this->template_html = 'path/to/html-template.php';
$this->template_plain = 'path/to/plain-template.php';
//$this->recipient = 'someone@example.com';
$this->customer_email = true;
parent::__construct();
$this->manual = true;
}
/**
* Trigger the sending of this email.
*
* @param int $order_id The order ID.
*/
public function trigger( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! is_a( $order, 'WC_Order' ) ) {
return;
}
$this->object = $order;
$this->recipient = $order->get_billing_email();
if ( $this->is_enabled() && $this->get_recipient() ) {
$this->setup_locale();
$this->send(
$this->get_recipient(),
$this->get_subject(),
$this->get_content(),
$this->get_headers(),
$this->get_attachments()
);
$this->restore_locale();
}
}
/**
* @return string
*/
public function get_content_html() {
return wc_get_template_html(
$this->template_html, array(
'order' => $this->object,
'email_heading' => $this->get_heading(),
'sent_to_admin' => false,
'plain_text' => false,
'email' => $this,
)
);
}
/**
* @return string
*/
public function get_content_plain() {
return wc_get_template_html(
$this->template_plain, array(
'order' => $this->object,
'email_heading' => $this->get_heading(),
'sent_to_admin' => false,
'plain_text' => true,
'email' => $this,
)
);
}
}
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
do_action( 'woocommerce_email_header', $email_heading, $email );
?>
<p>Write your HTML in here</p>
<?php
do_action( 'woocommerce_email_footer', $email );
<?php
// need to include this otherwise the WC_Email class can't be extended
include_once wc()->plugin_path() . '/includes/emails/class-wc-email.php';
add_filter( 'woocommerce_email_classes', function ( $registered_emails ) {
$registered_emails['Custom_Order_Email'] = new CustomOrderEmail();
return $registered_emails;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.