Background jobs in WordPress with Action Scheduler
WP-Cron is fine for “run this every hour”, but it is a poor queue: events are stored in one option, there is no history, and thousands of pending events slow every request. Action Scheduler, the library WooCommerce uses for its own background work, stores each job as a row in its own tables, runs them in batches and logs every run, visible under Tools, Scheduled Actions.
Problem: After each completed order a shop sends three emails: a thank-you note right away, a review request after seven days and a discount after thirty. With WP-Cron the events pile up, some are lost, and nobody can see what was sent.
Solution: Schedule one action per email with as_schedule_single_action() when the order is completed, use the order ID as argument and a group name, check as_has_scheduled_action() to avoid duplicates, handle each hook in a callback that re-checks the order, and cancel the pending actions with as_unschedule_all_actions() when the order is refunded.
Schedule the sequence
add_action( 'woocommerce_order_status_completed', 'ha_schedule_followups' );
function ha_schedule_followups( $order_id ) {
$jobs = [
'ha_send_thank_you' => MINUTE_IN_SECONDS,
'ha_send_review_request' => 7 * DAY_IN_SECONDS,
'ha_send_comeback_offer' => 30 * DAY_IN_SECONDS,
];
foreach ( $jobs as $hook => $delay ) {
$args = [ 'order_id' => (int) $order_id ];
if ( ! as_has_scheduled_action( $hook, $args, 'ha-followups' ) ) {
as_schedule_single_action( time() + $delay, $hook, $args, 'ha-followups' );
}
}
}
The arguments are stored with the action and passed to the callback; keep them small (IDs, not objects). The duplicate check makes it safe when an order is completed twice, for example after being reopened.
The callbacks
add_action( 'ha_send_review_request', function ( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order || ! $order->has_status( 'completed' ) || $order->get_meta( '_ha_review_sent' ) ) {
return;
}
$sent = wp_mail(
$order->get_billing_email(),
__( 'How do you like your order?', 'textdomain' ),
ha_review_email_body( $order )
);
if ( ! $sent ) {
throw new Exception( 'Review email for order ' . $order_id . ' could not be sent.' );
}
$order->update_meta_data( '_ha_review_sent', time() );
$order->save();
} );
The callback checks the order again at run time: a lot can happen in seven days. An exception marks the action as failed and logs the message in the action’s log, visible in the admin. The marker meta prevents a second email if the action is ever run twice.
Cancel when the order is refunded
add_action( 'woocommerce_order_status_refunded', function ( $order_id ) {
foreach ( [ 'ha_send_thank_you', 'ha_send_review_request', 'ha_send_comeback_offer' ] as $hook ) {
as_unschedule_all_actions( $hook, [ 'order_id' => (int) $order_id ], 'ha-followups' );
}
} );
Without WooCommerce
Action Scheduler is a standalone library. A plugin can bundle it with Composer (woocommerce/action-scheduler) and load action-scheduler.php early; when several plugins bundle it, the newest copy is used. It is triggered by WP-Cron and by an async request on admin pages, and on busy sites it can be run from the system cron with wp action-scheduler run. Single WP-Cron events for comparison: wp_schedule_single_event().
NOTE: Tools, Scheduled Actions shows pending, completed and failed actions with their logs; use it to check the first real orders. Completed actions are deleted after 30 days by default (filter action_scheduler_retention_period), so the table does not grow forever.