Moved email functions to classes, and made posts only save once (faster)

This commit is contained in:
Mike Jolley 2011-12-10 17:28:32 +00:00
parent 2f1eb1e975
commit 733b51401c
14 changed files with 522 additions and 485 deletions

View File

@ -524,4 +524,33 @@ function woocommerce_add_shortcode_tinymce_plugin($plugin_array) {
function woocommerce_refresh_mce($ver) {
$ver += 3;
return $ver;
}
/**
* Preview Emails
**/
add_action('admin_init', 'woocommerce_preview_emails');
function woocommerce_preview_emails() {
if (isset($_GET['preview_woocommerce_mail'])) :
$nonce = $_REQUEST['_wpnonce'];
if (!wp_verify_nonce($nonce, 'preview-mail') ) die('Security check');
global $woocommerce;
$mailer = $woocommerce->mailer();
$email_heading = __('Email preview', 'woothemes');
$message = '<h2>WooCommerce sit amet</h2>';
$message.= wpautop('Ut ut est qui euismod parum. Dolor veniam tation nihil assum mazim. Possim fiant habent decima et claritatem. Erat me usus gothica laoreet consequat. Clari facer litterarum aliquam insitam dolor.
Gothica minim lectores demonstraverunt ut soluta. Sequitur quam exerci veniam aliquip litterarum. Lius videntur nisl facilisis claritatem nunc. Praesent in iusto me tincidunt iusto. Dolore lectores sed putamus exerci est. ');
echo $mailer->wrap_message( $email_heading, $message );
exit;
endif;
}

View File

@ -548,14 +548,14 @@ function woocommerce_process_shop_order_meta( $post_id, $post ) {
$order->add_order_note( sprintf( __('Item #%s stock reduced from %s to %s.', 'woothemes'), $order_item['id'], $old_stock, $new_quantity) );
if ($new_quantity<0) :
do_action('woocommerce_product_on_backorder_notification', $order_item['id'], $values['quantity']);
do_action('woocommerce_product_on_backorder', array( 'product' => $order_item['id'], 'quantity' => $values['quantity']));
endif;
// stock status notifications
if (get_option('woocommerce_notify_no_stock_amount') && get_option('woocommerce_notify_no_stock_amount')>=$new_quantity) :
do_action('woocommerce_no_stock_notification', $order_item['id']);
do_action('woocommerce_no_stock', $order_item['id']);
elseif (get_option('woocommerce_notify_low_stock_amount') && get_option('woocommerce_notify_low_stock_amount')>=$new_quantity) :
do_action('woocommerce_low_stock_notification', $order_item['id']);
do_action('woocommerce_low_stock', $order_item['id']);
endif;
endif;
@ -603,7 +603,9 @@ function woocommerce_process_shop_order_meta( $post_id, $post ) {
elseif (isset($_POST['invoice']) && $_POST['invoice']) :
// Mail link to customer
woocommerce_pay_for_order_customer_notification( $order );
global $woocommerce;
$mailer = $woocommerce->mailer();
$mailer->pay_for_order_customer_notification( $order );
endif;

View File

@ -72,6 +72,8 @@ function woocommerce_meta_boxes_save( $post_id, $post ) {
global $wpdb;
if ( !$_POST ) return $post_id;
if ( is_int( wp_is_post_revision( $post_id ) ) ) return;
if( is_int( wp_is_post_autosave( $post_id ) ) ) return;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id;
if ( !isset($_POST['woocommerce_meta_nonce']) || (isset($_POST['woocommerce_meta_nonce']) && !wp_verify_nonce( $_POST['woocommerce_meta_nonce'], 'woocommerce_save_data' ))) return $post_id;
if ( !current_user_can( 'edit_post', $post_id )) return $post_id;

View File

@ -567,8 +567,8 @@ class woocommerce_checkout {
wp_update_user( array ('ID' => $user_id, 'role' => 'customer') ) ;
// send the user a confirmation and their login details
woocommerce_customer_new_account( $user_id, $user_pass );
//wp_new_user_notification( $user_id, $user_pass );
$mailer = $woocommerce->mailer();
$mailer->customer_new_account( $user_id, $password );
// set the WP login cookie
$secure_cookie = is_ssl() ? true : false;

View File

@ -469,7 +469,7 @@ class woocommerce_order {
add_comment_meta($comment_id, 'is_customer_note', $is_customer_note);
if ($is_customer_note) :
do_action( 'woocommerce_new_customer_note', $this->id, $note );
do_action( 'woocommerce_new_customer_note', array( 'order_id' => $this->id, 'customer_note' => $note ) );
endif;
return $comment_id;
@ -620,14 +620,14 @@ class woocommerce_order {
$this->add_order_note( sprintf( __('Item #%s stock reduced from %s to %s.', 'woothemes'), $item['id'], $old_stock, $new_quantity) );
if ($new_quantity<0) :
do_action('woocommerce_product_on_backorder_notification', $item['id'], $item['qty']);
do_action('woocommerce_product_on_backorder', array( 'product' => $order_item['id'], 'quantity' => $values['quantity']));
endif;
// stock status notifications
if (get_option('woocommerce_notify_no_stock_amount') && get_option('woocommerce_notify_no_stock_amount')>=$new_quantity) :
do_action('woocommerce_no_stock_notification', $item['id']);
do_action('woocommerce_no_stock', $item['id']);
elseif (get_option('woocommerce_notify_low_stock_amount') && get_option('woocommerce_notify_low_stock_amount')>=$new_quantity) :
do_action('woocommerce_low_stock_notification', $item['id']);
do_action('woocommerce_low_stock', $item['id']);
endif;
endif;

View File

@ -0,0 +1,373 @@
<?php
/**
* WooCommerce Emails Class
*
* @class woocommerce
* @package WooCommerce
* @category Class
* @author WooThemes
*/
class woocommerce_email {
private $_from_address;
private $_from_name;
/** constructor */
function __construct() {
$this->_from_name = get_option('woocommerce_email_from_name');
$this->_from_address = get_option('woocommerce_email_from_address');
/**
* Email Header + Footer
**/
add_action('woocommerce_email_header', array(&$this, 'email_header'));
add_action('woocommerce_email_footer', array(&$this, 'email_footer'));
/**
* Add order meta to email templates
**/
add_action('woocommerce_email_after_order_table', array(&$this, 'order_meta'), 10, 2);
/**
* Hooks for sending emails during store events
**/
add_action('woocommerce_low_stock_notification', array(&$this, 'low_stock'));
add_action('woocommerce_no_stock_notification', array(&$this, 'no_stock'));
add_action('woocommerce_product_on_backorder_notification', array(&$this, 'backorder'));
add_action('woocommerce_order_status_pending_to_processing_notification', array(&$this, 'new_order'));
add_action('woocommerce_order_status_pending_to_completed_notification', array(&$this, 'new_order'));
add_action('woocommerce_order_status_pending_to_on-hold_notification', array(&$this, 'new_order'));
add_action('woocommerce_order_status_failed_to_processing_notification', array(&$this, 'new_order'));
add_action('woocommerce_order_status_failed_to_completed_notification', array(&$this, 'new_order'));
add_action('woocommerce_order_status_pending_to_processing_notification', array(&$this, 'customer_processing_order'));
add_action('woocommerce_order_status_pending_to_on-hold_notification', array(&$this, 'customer_processing_order'));
add_action('woocommerce_order_status_completed_notification', array(&$this, 'customer_completed_order'));
add_action('woocommerce_new_customer_note_notification', array(&$this, 'customer_note'));
// Let 3rd parties unhook the above via this hook
do_action( 'woocommerce_email', $this );
}
function get_from_name() {
return $this->_from_name;
}
function get_from_address() {
return $this->_from_address;
}
function get_content_type() {
return 'text/html';
}
function email_header() {
woocommerce_get_template('emails/email_header.php', false);
}
function email_footer() {
woocommerce_get_template('emails/email_footer.php', false);
}
/**
* Wraps a message in the woocommerce mail template
**/
function wrap_message( $email_heading, $message ) {
// Buffer
ob_start();
do_action('woocommerce_email_header');
echo wpautop(wptexturize( $message ));
do_action('woocommerce_email_footer');
// Get contents
$message = ob_get_clean();
return $message;
}
function send( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = "" ) {
add_filter( 'wp_mail_from', array(&$this, 'get_from_address') );
add_filter( 'wp_mail_from_name', array(&$this, 'get_from_name') );
add_filter( 'wp_mail_content_type', array(&$this, 'get_content_type') );
// Send the mail
wp_mail( $to, $subject, $message, $headers, $attachments );
// Unhook
remove_filter( 'wp_mail_from', array(&$this, 'get_from_address') );
remove_filter( 'wp_mail_from_name', array(&$this, 'get_from_name') );
remove_filter( 'wp_mail_content_type', array(&$this, 'get_content_type') );
}
/**
* New order
**/
function new_order( $order_id ) {
global $order, $email_heading;
$email_heading = __('New Customer Order', 'woothemes');
$subject = sprintf(__('[%s] New Customer Order (# %s)', 'woothemes'), get_bloginfo('name'), $order_id);
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/new_order.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
$this->send( get_option('woocommerce_new_order_email_recipient'), $subject, $message );
}
/**
* Processing Order
**/
function customer_processing_order( $order_id ) {
global $order, $email_heading;
$order = &new woocommerce_order( $order_id );
$email_heading = __('Order Received', 'woothemes');
$subject = sprintf(__('[%s] Order Received', 'woothemes'), get_bloginfo('name'));
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/customer_processing_order.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
$this->send( $order->billing_email, $subject, $message );
}
/**
* Completed Order
**/
function customer_completed_order( $order_id ) {
global $order, $email_heading;
$order = &new woocommerce_order( $order_id );
if ($order->has_downloadable_item) :
$subject = __('[%s] Order Complete/Download Links', 'woothemes');
$email_heading = __('Order Complete/Download Links', 'woothemes');
else :
$subject = __('[%s] Order Complete', 'woothemes');
$email_heading = __('Order Complete', 'woothemes');
endif;
$email_heading = apply_filters('woocommerce_completed_order_customer_notification_subject', $email_heading);
$subject = sprintf($subject, get_bloginfo('name'));
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/customer_completed_order.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
$this->send( $order->billing_email, $subject, $message );
}
/**
* Pay for order
**/
function pay_for_order_customer_notification( $pay_for_order ) {
global $order, $email_heading;
$order = $pay_for_order;
$email_heading = sprintf(__('Invoice for Order #%s', 'woothemes'), $order->id);
$subject = sprintf(__('[%s] Pay for Order', 'woothemes'), get_bloginfo('name'));
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/customer_pay_for_order.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
$this->send( $order->billing_email, $subject, $message );
}
/**
* Customer notes
**/
function customer_note( $args ) {
global $order, $email_heading, $customer_note;
$defaults = array(
'order_id' => '',
'customer_note' => ''
);
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
if (!$order_id || !$customer_note) return;
$order = &new woocommerce_order( $order_id );
$email_heading = __('A note has been added to your order', 'woothemes');
$subject = sprintf(__('[%s] A note has been added to your order', 'woothemes'), get_bloginfo('name'));
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/customer_note_notification.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
$this->send( $order->billing_email, $subject, $message );
}
/**
* Low stock notification email
**/
function low_stock( $product ) {
$_product = &new woocommerce_product($product);
$subject = '[' . get_bloginfo('name') . '] ' . __('Product low in stock', 'woothemes');
$message = $this->wrap_message(
__('Product low in stock', 'woothemes'),
'#' . $_product->id .' '. $_product->get_title() . ' ('. $_product->sku.') ' . __('is low in stock.', 'woothemes')
);
// Send the mail
$this->send( get_option('woocommerce_stock_email_recipient'), $subject, $message );
}
/**
* No stock notification email
**/
function no_stock( $product ) {
$_product = &new woocommerce_product($product);
$subject = '[' . get_bloginfo('name') . '] ' . __('Product out of stock', 'woothemes');
$message = $this->wrap_message(
__('Product out of stock', 'woothemes'),
'#' . $_product->id .' '. $_product->get_title() . ' ('. $_product->sku.') ' . __('is out of stock.', 'woothemes')
);
// Send the mail
$this->send( get_option('woocommerce_stock_email_recipient'), $subject, $message );
}
/**
* Backorder notification email
**/
function backorder( $args ) {
$defaults = array(
'product' => '',
'quantity' => ''
);
$args = wp_parse_args( $args, $defaults );
extract( $args, EXTR_SKIP );
if (!$product || !$quantity) return;
$_product = &new woocommerce_product($product);
$subject = '[' . get_bloginfo('name') . '] ' . __('Product Backorder', 'woothemes');
$message = $this->wrap_message(
__('Product Backorder', 'woothemes'),
$quantity . __(' units of #', 'woothemes') . $_product->id .' '. $_product->get_title() . ' ('. $_product->sku.') ' . __('have been backordered.', 'woothemes')
);
// Send the mail
$this->send( get_option('woocommerce_stock_email_recipient'), $subject, $message );
}
/**
* Add order meta to email templates
**/
function order_meta( $order, $sent_to_admin ) {
$meta = array();
$show_fields = apply_filters('woocommerce_email_order_meta_keys', array('coupons'), $sent_to_admin);
if ($order->customer_note) :
$meta[__('Note:', 'woothemes')] = wptexturize($order->customer_note);
endif;
if ($show_fields) foreach ($show_fields as $field) :
$value = get_post_meta( $order->id, $field, true );
if ($value) $meta[ucwords(esc_attr($field))] = wptexturize($value);
endforeach;
if (sizeof($meta)>0) :
echo '<h2>'.__('Order information', 'woothemes').'</h2>';
foreach ($meta as $key=>$value) :
echo '<p><strong>'.$key.':</strong> '.$value.'</p>';
endforeach;
endif;
}
/**
* Customer new account welcome email
**/
function customer_new_account( $user_id, $plaintext_pass ) {
global $user_login, $user_pass, $blogname;
if (!$user_id || !$plaintext_pass) return;
$user = new WP_User($user_id);
$user_login = stripslashes($user->user_login);
$user_email = stripslashes($user->user_email);
$user_pass = $plaintext_pass;
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$subject = sprintf(__('Your account on %s', 'woothemes'), $blogname);
$email_heading = __('Your account details', 'woothemes');
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/customer_new_account.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
$this->send( $user_email, $subject, $message );
}
}

View File

@ -1,6 +1,6 @@
<?php if (!defined('ABSPATH')) exit; ?>
<?php global $order_id, $woocommerce; $order = &new woocommerce_order( $order_id ); ?>
<?php global $order_id, $order, $woocommerce; ?>
<?php do_action('woocommerce_email_header'); ?>

View File

@ -1,6 +1,6 @@
<?php if (!defined('ABSPATH')) exit; ?>
<?php global $order_id, $woocommerce, $customer_note; $order = &new woocommerce_order( $order_id ); ?>
<?php global $order_id, $order, $customer_note, $woocommerce; ?>
<?php do_action('woocommerce_email_header'); ?>

View File

@ -1,6 +1,6 @@
<?php if (!defined('ABSPATH')) exit; ?>
<?php global $order_id, $order, $woocommerce; ?>
<?php global $order, $woocommerce; ?>
<?php do_action('woocommerce_email_header'); ?>

View File

@ -1,6 +1,6 @@
<?php if (!defined('ABSPATH')) exit; ?>
<?php global $order_id, $woocommerce; $order = &new woocommerce_order( $order_id ); ?>
<?php global $order_id, $order, $woocommerce; ?>
<?php do_action('woocommerce_email_header'); ?>

View File

@ -48,6 +48,7 @@ class woocommerce {
var $payment_gateways;
var $countries;
var $validation;
var $woocommerce_email;
/** Taxonomies ************************************************************/
@ -98,6 +99,11 @@ class woocommerce {
add_action( 'plugins_loaded', array( &$this->shipping, 'init' ), 1); // Load shipping methods - some more may be added by plugins
add_action( 'plugins_loaded', array( &$this->payment_gateways, 'init' ), 1); // Load payment methods - some more may be added by plugins
// Email Actions
$email_actions = array( 'woocommerce_low_stock', 'woocommerce_no_stock', 'woocommerce_product_on_backorder', 'woocommerce_order_status_pending_to_processing', 'woocommerce_order_status_pending_to_completed', 'woocommerce_order_status_pending_to_on-hold', 'woocommerce_order_status_failed_to_processing', 'woocommerce_order_status_failed_to_completed', 'woocommerce_order_status_pending_to_processing', 'woocommerce_order_status_pending_to_on-hold', 'woocommerce_order_status_completed', 'woocommerce_new_customer_note' );
foreach ($email_actions as $action) add_action($action, array( &$this, 'send_transactional_email'));
// Actions for SSL
if (!is_admin()) :
add_action( 'wp', array( &$this, 'ssl_redirect'));
@ -151,7 +157,6 @@ class woocommerce {
include( 'woocommerce_functions.php' ); // Contains core functions for the front/back end
include( 'widgets/widgets-init.php' ); // Widget classes
include( 'woocommerce_emails.php' ); // Email template handlers
include( 'classes/countries.class.php' ); // Defines countries and states
include( 'classes/order.class.php' ); // Single order class
include( 'classes/product.class.php' ); // Product class
@ -808,7 +813,7 @@ class woocommerce {
/** Load Instances on demand **********************************************/
/**
* Checkout Class
* Get Checkout Class
*/
function checkout() {
if ( !class_exists('woocommerce_checkout') ) include( 'classes/checkout.class.php' );
@ -816,12 +821,31 @@ class woocommerce {
}
/**
* Logging Class
* Get Logging Class
*/
function logger() {
if ( !class_exists('woocommerce_logger') ) include( 'classes/woocommerce_logger.class.php' );
return new woocommerce_logger();
}
/**
* Email Class
*/
function send_transactional_email( $args = array() ) {
$this->mailer();
do_action( current_filter() . '_notification' , $args );
}
function mailer() {
// Init mail class
if ( !class_exists('woocommerce_email') ) :
include( 'classes/woocommerce_email.class.php' );
$this->woocommerce_email = &new woocommerce_email();
endif;
return $this->woocommerce_email;
}
/** Helper functions ******************************************************/

View File

@ -486,9 +486,10 @@ function woocommerce_process_registration() {
// Change role
wp_update_user( array ('ID' => $user_id, 'role' => 'customer') ) ;
// send the user a confirmation and their login details
woocommerce_customer_new_account( $user_id, $password );
// send the user a confirmation and their login details
$mailer = $woocommerce->mailer();
$mailer->customer_new_account( $user_id, $password );
// set the WP login cookie
$secure_cookie = is_ssl() ? true : false;

View File

@ -1,465 +0,0 @@
<?php
/**
* WooCommerce Emails
*
* Email handling for important shop events.
*
* @package WooCommerce
* @category Emails
* @author WooThemes
*/
/**
* Mail from name/email
**/
function woocommerce_mail_from_name( $name ) {
return get_option('woocommerce_email_from_name');
}
function woocommerce_mail_from( $email ) {
return get_option('woocommerce_email_from_address');
}
/**
* HTML emails from WooCommerce
**/
function woocommerce_mail( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = "" ) {
add_filter( 'wp_mail_from', 'woocommerce_mail_from' );
add_filter( 'wp_mail_from_name', 'woocommerce_mail_from_name' );
add_filter( 'wp_mail_content_type', 'woocommerce_email_content_type' );
// Send the mail
wp_mail( $to, $subject, $message, $headers, $attachments );
// Unhook
remove_filter( 'wp_mail_from', 'woocommerce_mail_from' );
remove_filter( 'wp_mail_from_name', 'woocommerce_mail_from_name' );
remove_filter( 'wp_mail_content_type', 'woocommerce_email_content_type' );
}
/**
* Wraps a message in the woocommerce mail template
**/
function woocommerce_mail_template( $heading, $message ) {
global $email_heading;
$email_heading = $heading;
// Buffer
ob_start();
do_action('woocommerce_email_header');
echo wpautop(wptexturize( $message ));
do_action('woocommerce_email_footer');
// Get contents
$message = ob_get_clean();
return $message;
}
/**
* Email Header
**/
add_action('woocommerce_email_header', 'woocommerce_email_header');
function woocommerce_email_header() {
woocommerce_get_template('emails/email_header.php', false);
}
/**
* Email Footer
**/
add_action('woocommerce_email_footer', 'woocommerce_email_footer');
function woocommerce_email_footer() {
woocommerce_get_template('emails/email_footer.php', false);
}
/**
* HTML email type
**/
function woocommerce_email_content_type($content_type){
return 'text/html';
}
/**
* Hooks for emails
**/
add_action('woocommerce_low_stock_notification', 'woocommerce_low_stock_notification');
add_action('woocommerce_no_stock_notification', 'woocommerce_no_stock_notification');
add_action('woocommerce_product_on_backorder_notification', 'woocommerce_product_on_backorder_notification', 1, 2);
/**
* New order notification email template
**/
add_action('woocommerce_order_status_pending_to_processing', 'woocommerce_new_order_notification');
add_action('woocommerce_order_status_pending_to_completed', 'woocommerce_new_order_notification');
add_action('woocommerce_order_status_pending_to_on-hold', 'woocommerce_new_order_notification');
add_action('woocommerce_order_status_failed_to_processing', 'woocommerce_new_order_notification');
add_action('woocommerce_order_status_failed_to_completed', 'woocommerce_new_order_notification');
function woocommerce_new_order_notification( $id ) {
global $order_id, $email_heading;
$order_id = $id;
$email_heading = __('New Customer Order', 'woothemes');
$subject = sprintf(__('[%s] New Customer Order (# %s)', 'woothemes'), get_bloginfo('name'), $order_id);
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/new_order.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
woocommerce_mail( get_option('woocommerce_new_order_email_recipient'), $subject, $message );
}
/**
* Processing order notification email template
**/
add_action('woocommerce_order_status_pending_to_processing', 'woocommerce_processing_order_customer_notification');
add_action('woocommerce_order_status_pending_to_on-hold', 'woocommerce_processing_order_customer_notification');
function woocommerce_processing_order_customer_notification( $id ) {
global $order_id, $email_heading;
$order_id = $id;
$order = &new woocommerce_order( $order_id );
$email_heading = __('Order Received', 'woothemes');
$subject = sprintf(__('[%s] Order Received', 'woothemes'), get_bloginfo('name'));
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/customer_processing_order.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
woocommerce_mail( $order->billing_email, $subject, $message );
}
/**
* Completed order notification email template - this one includes download links for downloadable products
**/
add_action('woocommerce_order_status_completed', 'woocommerce_completed_order_customer_notification');
function woocommerce_completed_order_customer_notification( $id ) {
global $order_id, $email_heading;
$order_id = $id;
$order = &new woocommerce_order( $order_id );
if ($order->has_downloadable_item()) :
$subject = __('[%s] Order Complete/Download Links', 'woothemes');
$email_heading = __('Order Complete/Download Links', 'woothemes');
else :
$subject = __('[%s] Order Complete', 'woothemes');
$email_heading = __('Order Complete', 'woothemes');
endif;
$email_heading = apply_filters('woocommerce_completed_order_customer_notification_subject', $email_heading);
$subject = sprintf($subject, get_bloginfo('name'));
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/customer_completed_order.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
woocommerce_mail( $order->billing_email, $subject, $message );
}
/**
* Pay for order notification email template - this one includes a payment link
**/
function woocommerce_pay_for_order_customer_notification( $the_order ) {
global $order_id, $order, $email_heading;
$order = $the_order;
$order_id = $order->id;
$email_heading = sprintf(__('Invoice for Order #%s', 'woothemes'), $order_id);
$subject = sprintf(__('[%s] Pay for Order', 'woothemes'), get_bloginfo('name'));
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/customer_pay_for_order.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
woocommerce_mail( $order->billing_email, $subject, $message );
}
/**
* Customer note notification
**/
add_action('woocommerce_new_customer_note', 'woocommerce_customer_note_notification', 10, 2);
function woocommerce_customer_note_notification( $id, $note ) {
global $order_id, $email_heading, $customer_note;
$order_id = $id;
$customer_note = $note;
$order = &new woocommerce_order( $order_id );
if (!$customer_note) return;
$email_heading = __('A note has been added to your order', 'woothemes');
$subject = sprintf(__('[%s] A note has been added to your order', 'woothemes'), get_bloginfo('name'));
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/customer_note_notification.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
woocommerce_mail( $order->billing_email, $subject, $message );
}
/**
* Low stock notification email
**/
function woocommerce_low_stock_notification( $product ) {
$_product = &new woocommerce_product($product);
$subject = '[' . get_bloginfo('name') . '] ' . __('Product low in stock', 'woothemes');
$message = woocommerce_mail_template(
__('Product low in stock', 'woothemes'),
'#' . $_product->id .' '. $_product->get_title() . ' ('. $_product->sku.') ' . __('is low in stock.', 'woothemes')
);
// Send the mail
woocommerce_mail( get_option('woocommerce_stock_email_recipient'), $subject, $message );
}
/**
* No stock notification email
**/
function woocommerce_no_stock_notification( $product ) {
$_product = &new woocommerce_product($product);
$subject = '[' . get_bloginfo('name') . '] ' . __('Product out of stock', 'woothemes');
$message = woocommerce_mail_template(
__('Product out of stock', 'woothemes'),
'#' . $_product->id .' '. $_product->get_title() . ' ('. $_product->sku.') ' . __('is out of stock.', 'woothemes')
);
// Send the mail
woocommerce_mail( get_option('woocommerce_stock_email_recipient'), $subject, $message );
}
/**
* Backorder notification email
**/
function woocommerce_product_on_backorder_notification( $product, $amount ) {
$_product = &new woocommerce_product($product);
$subject = '[' . get_bloginfo('name') . '] ' . __('Product Backorder', 'woothemes');
$message = woocommerce_mail_template(
__('Product Backorder', 'woothemes'),
$amount . __(' units of #', 'woothemes') . $_product->id .' '. $_product->get_title() . ' ('. $_product->sku.') ' . __('have been backordered.', 'woothemes')
);
// Send the mail
woocommerce_mail( get_option('woocommerce_stock_email_recipient'), $subject, $message );
}
/**
* Preview Emails
**/
add_action('admin_init', 'woocommerce_preview_emails');
function woocommerce_preview_emails() {
if (isset($_GET['preview_woocommerce_mail'])) :
$nonce = $_REQUEST['_wpnonce'];
if (!wp_verify_nonce($nonce, 'preview-mail') ) die('Security check');
global $email_heading;
$email_heading = __('Email preview', 'woothemes');
do_action('woocommerce_email_header');
echo '<h2>WooCommerce sit amet</h2>';
echo wpautop('Ut ut est qui euismod parum. Dolor veniam tation nihil assum mazim. Possim fiant habent decima et claritatem. Erat me usus gothica laoreet consequat. Clari facer litterarum aliquam insitam dolor.
Gothica minim lectores demonstraverunt ut soluta. Sequitur quam exerci veniam aliquip litterarum. Lius videntur nisl facilisis claritatem nunc. Praesent in iusto me tincidunt iusto. Dolore lectores sed putamus exerci est. ');
do_action('woocommerce_email_footer');
exit;
endif;
}
/**
* Add order meta to email templates
**/
add_action('woocommerce_email_after_order_table', 'woocommerce_email_order_meta', 10, 2);
function woocommerce_email_order_meta( $order, $sent_to_admin ) {
$meta = array();
$show_fields = apply_filters('woocommerce_email_order_meta_keys', array('coupons'), $sent_to_admin);
if ($order->customer_note) :
$meta[__('Note:', 'woothemes')] = wptexturize($order->customer_note);
endif;
if ($show_fields) foreach ($show_fields as $field) :
$value = get_post_meta( $order->id, $field, true );
if ($value) $meta[ucwords(esc_attr($field))] = wptexturize($value);
endforeach;
if (sizeof($meta)>0) :
echo '<h2>'.__('Order information', 'woothemes').'</h2>';
foreach ($meta as $key=>$value) :
echo '<p><strong>'.$key.':</strong> '.$value.'</p>';
endforeach;
endif;
}
/**
* Customer new account welcome email
**/
function woocommerce_customer_new_account( $user_id, $plaintext_pass ) {
global $email_heading, $user_login, $user_pass, $blogname;
if ( empty($plaintext_pass) ) return;
$user = new WP_User($user_id);
$user_login = stripslashes($user->user_login);
$user_email = stripslashes($user->user_email);
$user_pass = $plaintext_pass;
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
$subject = sprintf(__('Your account on %s', 'woothemes'), $blogname);
$email_heading = __('Your account details', 'woothemes');
// Buffer
ob_start();
// Get mail template
woocommerce_get_template('emails/customer_new_account.php', false);
// Get contents
$message = ob_get_clean();
// Send the mail
woocommerce_mail( $user_email, $subject, $message );
}
/**
* Hex darker/lighter/contrast functions for colours
**/
if (!function_exists('woocommerce_hex_darker')) {
function woocommerce_hex_darker( $color, $factor = 30 ) {
$color = str_replace('#', '', $color);
$base['R'] = hexdec($color{0}.$color{1});
$base['G'] = hexdec($color{2}.$color{3});
$base['B'] = hexdec($color{4}.$color{5});
$color = '#';
foreach ($base as $k => $v) :
$amount = $v / 100;
$amount = round($amount * $factor);
$new_decimal = $v - $amount;
$new_hex_component = dechex($new_decimal);
if(strlen($new_hex_component) < 2) :
$new_hex_component = "0".$new_hex_component;
endif;
$color .= $new_hex_component;
endforeach;
return $color;
}
}
if (!function_exists('woocommerce_hex_lighter')) {
function woocommerce_hex_lighter( $color, $factor = 30 ) {
$color = str_replace('#', '', $color);
$base['R'] = hexdec($color{0}.$color{1});
$base['G'] = hexdec($color{2}.$color{3});
$base['B'] = hexdec($color{4}.$color{5});
$color = '#';
foreach ($base as $k => $v) :
$amount = 255 - $v;
$amount = $amount / 100;
$amount = round($amount * $factor);
$new_decimal = $v + $amount;
$new_hex_component = dechex($new_decimal);
if(strlen($new_hex_component) < 2) :
$new_hex_component = "0".$new_hex_component;
endif;
$color .= $new_hex_component;
endforeach;
return $color;
}
}
if (!function_exists('woocommerce_light_or_dark')) {
function woocommerce_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) {
return (hexdec($color) > 0xffffff/2) ? $dark : $light;
}
}

View File

@ -9,6 +9,17 @@
* @author WooThemes
*/
/**
* HTML emails from WooCommerce
**/
function woocommerce_mail( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = "" ) {
global $woocommerce;
$mailer = $woocommerce->mailer();
$mailer->send( $to, $subject, $message, $headers, $attachments );
}
/**
* WooCommerce conditionals
*
@ -219,6 +230,66 @@ function woocommerce_get_formatted_variation( $variation = '', $flat = false ) {
endif;
}
/**
* Hex darker/lighter/contrast functions for colours
**/
if (!function_exists('woocommerce_hex_darker')) {
function woocommerce_hex_darker( $color, $factor = 30 ) {
$color = str_replace('#', '', $color);
$base['R'] = hexdec($color{0}.$color{1});
$base['G'] = hexdec($color{2}.$color{3});
$base['B'] = hexdec($color{4}.$color{5});
$color = '#';
foreach ($base as $k => $v) :
$amount = $v / 100;
$amount = round($amount * $factor);
$new_decimal = $v - $amount;
$new_hex_component = dechex($new_decimal);
if(strlen($new_hex_component) < 2) :
$new_hex_component = "0".$new_hex_component;
endif;
$color .= $new_hex_component;
endforeach;
return $color;
}
}
if (!function_exists('woocommerce_hex_lighter')) {
function woocommerce_hex_lighter( $color, $factor = 30 ) {
$color = str_replace('#', '', $color);
$base['R'] = hexdec($color{0}.$color{1});
$base['G'] = hexdec($color{2}.$color{3});
$base['B'] = hexdec($color{4}.$color{5});
$color = '#';
foreach ($base as $k => $v) :
$amount = 255 - $v;
$amount = $amount / 100;
$amount = round($amount * $factor);
$new_decimal = $v + $amount;
$new_hex_component = dechex($new_decimal);
if(strlen($new_hex_component) < 2) :
$new_hex_component = "0".$new_hex_component;
endif;
$color .= $new_hex_component;
endforeach;
return $color;
}
}
if (!function_exists('woocommerce_light_or_dark')) {
function woocommerce_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) {
return (hexdec($color) > 0xffffff/2) ? $dark : $light;
}
}
/**
* Exclude order comments from queries and RSS
*