Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active October 25, 2017 10:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save damiencarbery/7d4640e87ea3beefb5a16557ad166a49 to your computer and use it in GitHub Desktop.
Save damiencarbery/7d4640e87ea3beefb5a16557ad166a49 to your computer and use it in GitHub Desktop.
Add custom email to WooCommerce
<?php
/**
* An email to ask the vendor to check availability of a booking.
*
* @extends \WC_Email
*
* Code copied from: https://www.skyverge.com/blog/how-to-add-a-custom-woocommerce-email/
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WC_Check_Availability_Email extends WC_Email {
/**
* Set email defaults
*
* @since 0.1
*/
public function __construct() {
// set ID, this simply needs to be a unique name
$this->id = 'wc_vendor_check_availability';
// this is the title in WooCommerce Email settings
$this->title = 'Vendor Check Availability';
// this is the description in WooCommerce email settings
$this->description = 'Send email to the vendor when an order with a booking calendar is received. Vendor will check availability.';
// these are the default heading and subject lines that can be overridden using the settings
$this->heading = 'Vendor Check Availability';
$this->subject = '[{site_title}] Order: ({order_number}) - Check Date Availability for {product_title}';
// Booking information for the product.
// TODO: Retrieve this info from the order meta.
$this->product_name = 'NONE';
$this->booking_info = 'NONE';
// Find/replace
$this->find['product_name'] = '{product_title}';
$this->replace['product_name'] = $this->product_name; //'TODO:Product_Name';
// 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/vendor-check-availability.php';
// Don't bother with a plain email - being lazy.
$this->template_plain = 'emails/vendor-check-availability.php';
//$this->template_plain = 'emails/plain/admin-new-order.php';;
// Trigger when a new order is received.
add_action( 'woocommerce_order_status_processing', 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 = get_option( 'admin_email' ); // TODO: Replace with vendor information.
}
/**
* 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 );
$items = $this->object->get_items();
foreach($items as $item) {
if ('line_item' == $item['type']) {
$_product = get_product($item['product_id']);
$item_meta = new WC_Order_Item_Meta( $item, $_product );
if ($_product->is_virtual()) {
$calendar_or_time_left = get_field('calendar_or_time_left', $_product->id);
if (!$calendar_or_time_left) { $calendar_or_time_left = 'time_left'; } // Default value.
// A product with a booking calendar needs to be confirmed by vendor before it can be marked as completed.
if ($calendar_or_time_left == 'calendar') {
$vendor_email = get_field('contact_email', $_product->id);
if ($vendor_email) {
$this->recipient = $vendor_email;
$this->product_name = get_the_title($item['product_id']);
// replace variables in the subject/headings
$this->find[] = '{product_name}';
$this->replace[] = $this->product_name;
$this->find[] = '{order_number}';
$this->replace[] = $this->object->get_order_number();
if (!empty($item_meta->meta)) {
$this->booking_info = nl2br($item_meta->display(true, true, '_', "\n"));
$this->find[] = '{booking_info}';
$this->replace[] = $this->booking_info;
}
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() {
return wc_get_template_html( $this->template_html, array(
'order' => $this->object,
'email_heading' => $this->get_heading(),
'sent_to_admin' => true,
'plain_text' => false,
'email' => $this,
// Send additional info to the email template.
'booking_info' => $this->booking_info,
'product_name' => $this->product_name,
) );
}
function get_heading() {
return $this->format_string( $this->heading );
}
/**
* get_content_plain function.
*
* @since 0.1
* @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' => true,
'plain_text' => false,
'email' => $this,
'booking_info' => $this->booking_info,
'product_name' => $this->product_name,
) );
}
/**
* Initialize Settings Form Fields
*
* @since 0.1
*/
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',
'html' => 'HTML', 'woocommerce',
'multipart' => 'Multipart', 'woocommerce',
)
)
);
}
} // end \WC_Check_Availability_Email class
<?php
/**
* Add a custom email to the list of emails WooCommerce should load
*/
function add_check_availability_woocommerce_email( $email_classes ) {
// include our custom email class
require_once( dirname( __FILE__ ).'/class-wc-check-availability-email.php' );
// add the email class to the list of email classes that WooCommerce loads
$email_classes['WC_Check_Availability_Email'] = new WC_Check_Availability_Email();
return $email_classes;
}
add_filter( 'woocommerce_email_classes', 'add_check_availability_woocommerce_email' );
<?php
/**
* Vendor Check Availability
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* @hooked WC_Emails::email_header() Output the email header
*/
do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
<style>
.buttons { margin-top: 3em; }
.button { border-radius: 0; color:#fff; display: inline-block; padding:10px; text-decoration:none; text-align: center; text-transform:uppercase; width: 15em; }
.button-yes { background-color: green; }
.button-no { background-color: red; }
</style>
<p>Hi,</p>
<p>We have received an order for your <strong><?php echo $product_name; ?></strong>.</p>
<p>Please check the availability and click the appropriate button at the bottom of this email.</p>
<p>Order ID: <?php echo $order->id; ?></p>
<?php
echo '<p>Booking info:<br/>', $booking_info, '</p>';
echo "<p>Heading: $email_heading</p>";
echo "<p>Product Name: $product_name</p>";
// TODO: Write a standalone script that will change the status of the order.
$booking_yes_url = $site_url;
$booking_no_url = $site_url;
?>
<div class="buttons">
<p><a class="button button-yes" href="<?php echo $booking_yes_url; ?>">Yes, booking accepted</a></p>
<p><a class="button button-no" href="<?php echo $booking_no_url; ?>">No, date unavailable</a></p>
</div>
<?php
/**
* @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