Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This file contains the code to include email classes and trigger custom emails.
<?php
/**
* Handles email sending
*/
class Custom_Email_Manager {
/**
* Constructor sets up actions
*/
public function __construct() {
// template path
define( 'CUSTOM_TEMPLATE_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/' );
// hook for when order status is changed
add_action( 'woocommerce_order_status_pending', array( &$this, 'custom_trigger_email_action' ), 10, 2 );
// include the email class files
add_filter( 'woocommerce_email_classes', array( &$this, 'custom_init_emails' ) );
// Email Actions - Triggers
$email_actions = array(
'custom_pending_email',
'custom_item_email',
);
foreach ( $email_actions as $action ) {
add_action( $action, array( 'WC_Emails', 'send_transactional_email' ), 10, 10 );
}
add_filter( 'woocommerce_template_directory', array( $this, 'custom_template_directory' ), 10, 2 );
}
public function custom_init_emails( $emails ) {
// Include the email class file if it's not included already
if ( ! isset( $emails[ 'Custom_Email' ] ) ) {
$emails[ 'Custom_Email' ] = include_once( 'emails/class-custom-email.php' );
}
return $emails;
}
public function custom_trigger_email_action( $order_id, $posted ) {
// add an action for our email trigger if the order id is valid
if ( isset( $order_id ) && 0 != $order_id ) {
new WC_Emails();
do_action( 'custom_pending_email_notification', $order_id );
}
}
public function custom_template_directory( $directory, $template ) {
// ensure the directory name is correct
if ( false !== strpos( $template, '-custom' ) ) {
return 'my-custom-email';
}
return $directory;
}
}// end of class
new Custom_Email_Manager();
?>
@Fil86

This comment has been minimized.

Copy link

commented Feb 6, 2019

Hi ! This class is interesting, I'd like to know these things:

  • Why are you using these 2 lines of code inside custom_trigger_email_action function?
    • $order = new WC_order( $order_id );
    • new WC_Emails(); // or, better, new WC_Emails::instance()
      $order is NOT used then later inside the function, and WC_Emails instance is NOT assigned to any varaible.
      Are these 2 lines really necessary?
  • Why you trigger a do_action (line 48) and there's no corresponding add_action for the same action inside this Class?
    Or the add_action call has to be used in another external Class?
  • I've noticed that you use add_action for 2 "CUSTOM" email actions ( 'custom_pending_email' and 'custom_item_email').
    Why there's not a corresponding do_action for these custom actions? Are they managed externally, too?

Thanks a lot in advance,
Phil

@pinalj

This comment has been minimized.

Copy link
Owner Author

commented Feb 11, 2019

Hi Phil,

  1. The WC_order object is not necessary. I've updated the code. Thanks for pointing it out.

  2. new WC_Emails - this is necessary to ensure our hooks are triggered.

  3. The add_action call for custom_pending_email_notification is present in another class file (class-custom-email.php). This code above is for the main email manager class. So assuming a scenario where you are creating more than 1 custom emails, it makes sense to create a separate class file for each of the custom emails and add their respective add_actions in their class files.

  4. The do_action for 'custom_pending_email' is present in the file above. Whereas the 'custom_item_email' is handled in the other file class-custom-email.php. Since the example sends a separate email for each item in the order, we need these 2. If you were to create an email which needs to be sent per order, the custom_item_email would not be needed.

I hope this helps make things a bit more clearer.

Pinal

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.