Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active February 21, 2024 21:31
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save igorbenic/4d03305eb5f6fc6dd76459a320765b67 to your computer and use it in GitHub Desktop.
Save igorbenic/4d03305eb5f6fc6dd76459a320765b67 to your computer and use it in GitHub Desktop.
How to create a Custom WooCommerce Email | https://www.ibenic.com/create-custom-woocommerce-email
<?php
/**
* Class Custom_WC_Email
*/
class Custom_WC_Email {
/**
* Custom_WC_Email constructor.
*/
public function __construct() {
// Filtering the emails and adding our own email.
add_filter( 'woocommerce_email_classes', array( $this, 'register_email' ), 90, 1 );
// Absolute path to the plugin folder.
define( 'CUSTOM_WC_EMAIL_PATH', plugin_dir_path( __FILE__ ) );
}
/**
* @param array $emails
*
* @return array
*/
public function register_email( $emails ) {
require_once 'emails/class-wc-customer-cancel-order.php';
$emails['WC_Customer_Cancel_Order'] = new WC_Customer_Cancel_Order();
return $emails;
}
}
new Custom_WC_Email();
<?php
class WC_Customer_Cancel_Order extends WC_Email {
// ...
/**
* Get content html.
*
* @access public
* @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
), '', $this->template_base );
}
/**
* Get content plain.
*
* @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
), '', $this->template_base );
}
}
<?php
/**
* Class WC_Customer_Cancel_Order
*/
class WC_Customer_Cancel_Order extends WC_Email {
// ...
/**
* Trigger Function that will send this email to the customer.
*
* @access public
* @return void
*/
function trigger( $order_id ) {
$this->object = wc_get_order( $order_id );
if ( version_compare( '3.0.0', WC()->version, '>' ) ) {
$order_email = $this->object->billing_email;
} else {
$order_email = $this->object->get_billing_email();
}
$this->recipient = $order_email;
if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
return;
}
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}
}
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'WC_Email' ) ) {
return;
}
/**
* Class WC_Customer_Cancel_Order
*/
class WC_Customer_Cancel_Order extends WC_Email {
/**
* Create an instance of the class.
*
* @access public
* @return void
*/
function __construct() {
// Email slug we can use to filter other data.
$this->id = 'wc_customer_cancelled_order';
$this->title = __( 'Cancelled Order to Customer', 'custom-wc-email' );
$this->description = __( 'An email sent to the customer when an order is cancelled.', 'custom-wc-email' );
// For admin area to let the user know we are sending this email to customers.
$this->customer_email = true;
$this->heading = __( 'Order Cancelled', 'custom-wc-email' );
// translators: placeholder is {blogname}, a variable that will be substituted when email is sent out
$this->subject = sprintf( _x( '[%s] Order Cancelled', 'default email subject for cancelled emails sent to the customer', 'custom-wc-email' ), '{blogname}' );
// Template paths.
$this->template_html = 'emails/wc-customer-cancelled-order.php';
$this->template_plain = 'emails/plain/wc-customer-cancelled-order.php';
$this->template_base = CUSTOM_WC_EMAIL_PATH . 'templates/';
// Action to which we hook onto to send the email.
add_action( 'woocommerce_order_status_pending_to_cancelled_notification', array( $this, 'trigger' ) );
add_action( 'woocommerce_order_status_on-hold_to_cancelled_notification', array( $this, 'trigger' ) );
parent::__construct();
}
}
<?php
/**
* Plugin Name: Custom WooCommerce Email
*/
if ( ! defined( 'ABSPATH' ) ) {
return;
}
<?php
/**
* Admin cancelled order email (plain text)
*
* This template can be overridden by copying it to yourtheme/woocommerce/emails/plain/admin-cancelled-order.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woothemes.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates/Emails/Plain
* @version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
echo "= " . $email_heading . " =\n\n";
echo sprintf( __( 'The order #%d has been cancelled. The order details:', 'woocommerce' ), $order->id ) . "\n\n";
echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n";
/**
* @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup.
* @since 2.5.0
*/
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );
echo "\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n";
/**
* @hooked WC_Emails::order_meta() Shows order meta data.
*/
do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );
/**
* @hooked WC_Emails::customer_details() Shows customer details
* @hooked WC_Emails::email_address() Shows email address
*/
do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email );
echo "\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n";
echo apply_filters( 'woocommerce_email_footer_text', get_option( 'woocommerce_email_footer_text' ) );
<?php
/**
* Cancelled Order sent to Customer.
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* @hooked WC_Emails::email_header() Output the email header
*/
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
<p><?php printf( __( 'The order #%d has been cancelled. Order Details:', 'woocommerce' ), $order->get_order_number() ); ?></p>
<?php
/**
* @hooked WC_Emails::order_details() Shows the order details table.
* @hooked WC_Emails::order_schema_markup() Adds Schema.org markup.
* @since 2.5.0
*/
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );
/**
* @hooked WC_Emails::order_meta() Shows order meta data.
*/
do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email );
/**
* @hooked WC_Emails::customer_details() Shows customer details
* @hooked WC_Emails::email_address() Shows email address
*/
do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email );
/**
* @hooked WC_Emails::email_footer() Output the email footer
*/
do_action( 'woocommerce_email_footer', $email );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment