Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active June 12, 2024 19:11
Show Gist options
  • Save igorbenic/8f8a1e953658f02672ac5599cb7da42e to your computer and use it in GitHub Desktop.
Save igorbenic/8f8a1e953658f02672ac5599cb7da42e to your computer and use it in GitHub Desktop.
Custom WooCommerce Emails Delay
<?php
add_action( 'woocommerce_order_status_completed', 'queue_email', 10, 10 );
/**
* Generic function that will queue any action as we see fit.
*/
function queue_email( ...$args ) {
$filter = current_filter(); //woocommerce_order_status_completed
$delay = false;
switch( filter ) {
case: 'woocommerce_order_status_completed';
$delay = 2 * HOUR_IN_SECODS;
break;
}
if ( ! $delay ) { return; }
as_schedule_single_action(
time() + $delay,
'send_queued_email',
[
'filter' => $filter,
'args' => func_get_args(),
]
);
}
<?php
add_filter( 'woocommerce_email_actions', 'remove_order_completed_email_action', 99 );
/**
* Remove actions we don't want emails to trigger
*/
function remove_order_completed_email_action( $actions ) {
$actions_to_remove = [ 'woocommerce_order_status_completed' ];
return array_diff( $actions, $actions_to_remove );
}
<?php
add_action( 'send_queued_email', 'send_email', 10, 2 );
function send_email( $filter, $args ) {
\WC_Emails::send_queued_transactional_email( $filter, $args );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment