Skip to content

Instantly share code, notes, and snippets.

@james-allan
Last active August 3, 2018 01:02
Show Gist options
  • Save james-allan/ce82236d40e7b9ee5a1577631bbf364d to your computer and use it in GitHub Desktop.
Save james-allan/ce82236d40e7b9ee5a1577631bbf364d to your computer and use it in GitHub Desktop.
Delay early renewal order emails until after dates have been updated
<?php
/**
* Plugin Name: Delay Early Renewal Order Emails
* Plugin URI: https://gist.github.com/james-allan/ce82236d40e7b9ee5a1577631bbf364d
* Description: Delays early renewal order emails until after the subscription dates have been updated.
* Author: James Allan
* Author URI: Prospress.com
* Version: 1.0
**/
class WCSCC_Delay_Early_Renewal_Order_Emails {
protected static $renewal_order_email_actions = array(
'woocommerce_order_status_pending_to_processing',
'woocommerce_order_status_pending_to_completed',
'woocommerce_order_status_failed_to_processing',
'woocommerce_order_status_failed_to_completed',
'woocommerce_order_status_completed',
);
protected static $delayed_early_renewal_order_emails = array();
public static function init() {
foreach ( self::$renewal_order_email_actions as $action ) {
add_action( $action, array( __CLASS__, 'maybe_delay_early_renewal_order_email' ), 5 );
}
// Hooked in on 15 so we send emails after WCS_Cart_Early_Renewal::maybe_update_dates()
add_action( 'subscriptions_activated_for_order', array( __CLASS__, 'send_any_delayed_renewal_emails' ), 15 );
}
public static function maybe_delay_early_renewal_order_email( $order_id ) {
if ( ! function_exists( 'wcs_order_contains_early_renewal' ) || ! wcs_order_contains_early_renewal( $order_id ) ) {
return;
}
// Prevent Subscriptions sending the email early.
remove_action( current_filter(), 'WC_Subscriptions_Email::send_renewal_order_email' );
// Record the order ID so we can send the email later.
self::$delayed_early_renewal_order_emails[ $order_id ] = current_filter();
}
public static function send_any_delayed_renewal_emails( $order_id ) {
// If we delayed the email for this order, send it now.
if ( isset( self::$delayed_early_renewal_order_emails[ $order_id ] ) ) {
do_action( self::$delayed_early_renewal_order_emails[ $order_id ] . '_renewal_notification', $order_id );
}
}
}
WCSCC_Delay_Early_Renewal_Order_Emails::init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment