Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active October 27, 2023 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save damiencarbery/8058c484fc5db2fc21df7a7c2805291f to your computer and use it in GitHub Desktop.
Save damiencarbery/8058c484fc5db2fc21df7a7c2805291f to your computer and use it in GitHub Desktop.
Defer WooCommerce Advanced Notifications emails for a few minutes - Delay the sending of emails from the Advanced Notification plugin. https://www.damiencarbery.com/2023/10/defer-woocommerce-advanced-notifications-emails-for-a-few-minutes/
<?php
/*
Plugin Name: Defer WooCommerce Advanced Notifications emails for a few minutes
Plugin URI: https://www.damiencarbery.com/2023/10/defer-woocommerce-advanced-notifications-emails-for-a-few-minutes/
Description: Delay the sending of emails from the Advanced Notification plugin for a specified time after the normal delivery time. Use Action Scheduler instead of WP Cron.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.1
*/
class DeferWooAdvNotificationEmails {
private static $instance;
private $default_defer_time;
// Returns an instance of this class.
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = self();
}
return self::$instance;
}
// Initialize the plugin variables.
public function __construct() {
$this->default_defer_time = 120; // Defer for 600 seconds (10 minutes).
// Can add other email IDs and their defer times.
// You can uncomment the add_action() with 'order_status_changed' function to find out the before and after status.
// This is also the list of emails that will be deferred.
// Wait until other plugins loaded so that we can check for some specific plugins.
add_action( 'plugins_loaded', array( $this, 'init' ) );
}
// Set up WordPress specfic actions.
public function init() {
// This is the scheduled function that will send the email.
add_action( 'send_deferred_wooadvnotifications_email', array( $this, 'send_deferred_wooadvnotifications_email' ), 10, 2 );
// DEBUG: Add the order modification time and current time to prove that the email was intentionally delayed.
//add_action( 'woocommerce_email_customer_details', array( $this, 'add_defer_length_info_to_order_email' ), 20, 4 );
// Disable the WooCommerce Advanced Notifications that us its new_order() function.
add_filter( 'woocommerce_advanced_notifications_purchase_pending_to_processing', '__return_false' );
add_filter( 'woocommerce_advanced_notifications_purchase_pending_to_completed', '__return_false' );
add_filter( 'woocommerce_advanced_notifications_purchase_on-hold_to_processing', '__return_false' );
add_filter( 'woocommerce_advanced_notifications_purchase_on-hold_to_completed', '__return_false' );
add_filter( 'woocommerce_advanced_notifications_purchase_failed_to_processing', '__return_false' );
add_filter( 'woocommerce_advanced_notifications_purchase_failed_to_completed', '__return_false' );
// Hook into the WooCommerce equivalent actions.
add_action( 'woocommerce_order_status_pending_to_processing', array( $this, 'defer_new_order' ) );
add_action( 'woocommerce_order_status_pending_to_completed', array( $this, 'defer_new_order' ) );
add_action( 'woocommerce_order_status_on-hold_to_processing', array( $this, 'defer_new_order' ) );
add_action( 'woocommerce_order_status_on-hold_to_completed', array( $this, 'defer_new_order' ) );
add_action( 'woocommerce_order_status_failed_to_processing', array( $this, 'defer_new_order' ) );
add_action( 'woocommerce_order_status_failed_to_completed', array( $this, 'defer_new_order' ) );
}
public function defer_new_order( $order_id ) {
//error_log( 'defer_new_order: $order_id: ' . $order_id );
$args = array( 'order_id' => $order_id );
//error_log( 'defer_new_order: $args: ' . var_export( $args, true ) );
as_schedule_single_action( time() + $this->default_defer_time, 'send_deferred_wooadvnotifications_email', array( $args ) );
}
// Send the deferred email for order $order_id.
public function send_deferred_wooadvnotifications_email( $args ) {
//error_log( 'send_deferred_wooadvnotifications_email received $args: ' . var_export( $args, true ) );
$order_id = is_array( $args ) ? $args['order_id'] : $args;
$GLOBALS['wc_advanced_notifications']->new_order( $order_id );
}
// This is an experimental function to add the date/time the order was modified and
// the date/time the email was sent into email - to demonstrate that the deferring code worked.
public function add_defer_length_info_to_order_email( $order, $sent_to_admin, $plain_text, $email ) {
if ( !is_object( $email ) ) { return; } // Ensure $email is an object before testing $email->id.
//if ( 'customer_completed_order' == $email->id ) {
if ( true ) {
if ( $plain_text ) {
printf( '%s%s: The order was modified at %s and email sent at %s.', "\n", get_class( $email ), $order->get_date_modified(), current_time( 'mysql' ) );
}
else {
printf( '<p>%s: The order was modified at <strong>%s</strong> and email sent at <strong>%s</strong>.</p>', get_class( $email ), $order->get_date_modified(), current_time( 'mysql' ) );
}
}
}
}
$DeferWooAdvNotificationEmails = new DeferWooAdvNotificationEmails();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment