Last active
June 12, 2024 19:11
-
-
Save igorbenic/8f8a1e953658f02672ac5599cb7da42e to your computer and use it in GitHub Desktop.
Custom WooCommerce Emails Delay
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(), | |
] | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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