-
-
Save pinalj/c1e6336be4964efd66ece7677f361702 to your computer and use it in GitHub Desktop.
<?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(); | |
?> |
Hi Phil,
-
The WC_order object is not necessary. I've updated the code. Thanks for pointing it out.
-
new WC_Emails - this is necessary to ensure our hooks are triggered.
-
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. -
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
Hi ! This class is interesting, I'd like to know these things:
$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?
Or the add_action call has to be used in another external Class?
Why there's not a corresponding do_action for these custom actions? Are they managed externally, too?
Thanks a lot in advance,
Phil