
Scheduled actions in WooCommerce: the essential guide
When working with WordPress and WooCommerce, sooner or later you’ll need to run background tasks. Whether it’s sending an automatic email, processing recurring payments, or cleaning up old data, you need an efficient way to schedule tasks. While many developers use WP_Cron
, today we’ll show you why Scheduled Actions are a better choice, especially with WooCommerce.
What are Scheduled actions?
Scheduled Actions are a more reliable and powerful way to schedule background tasks in WordPress. Unlike WP_Cron
, which relies on website traffic to trigger tasks, scheduled actions are managed by Action Scheduler, a task queue library included with WooCommerce.
This system stores pending tasks in the database, runs them in the background, and retries them automatically if something fails. It also provides a user interface where you can view, cancel, or reschedule actions from the WordPress admin.
Why Use Scheduled Actions Instead of WP_Cron?
Feature | WP_Cron | Scheduled Actions (Action Scheduler) |
---|---|---|
Reliability | Depends on site traffic | Runs with separate processes or CLI |
Persistence | Does not store results | Saves actions and statuses in DB |
Admin management | Not available | UI available in WooCommerce |
Automatic retries | No | Yes, configurable |
Scalability | Limited | Designed for high-volume sites |
How does WooCommerce use it?
WooCommerce uses scheduled actions for almost everything that happens in the background, such as:
- Sending automated emails
- Cleaning up expired cart sessions
- Processing subscription renewals and payments
- Generating reports or syncing with external services
You can view scheduled actions in your site by going to: WooCommerce > Status > Scheduled Actions
How to create your own scheduled actions
1. Schedule a one-time action
<?php if ( function_exists( 'as_schedule_single_action' ) ) { as_schedule_single_action( strtotime( '+10 minutes' ), // Execution time 'send_welcome_email', // Hook name [ 'userId' => 123 ] // Optional args ); }
2. Register the callback
<?php add_action( 'send_welcome_email', 'sendWelcomeEmailCallback' ); function sendWelcomeEmailCallback( $args ) { $userId = $args['userId']; $userData = get_userdata( $userId ); wp_mail( $userData->user_email, 'Welcome!', 'Thanks for joining our platform.' ); }
3. Schedule a recurring task
<?php if ( function_exists( 'as_schedule_recurring_action' ) ) { as_schedule_recurring_action( strtotime( 'tomorrow 00:00:00' ), // First run DAY_IN_SECONDS, // Interval 'clean_temp_data' ); }
And its callback:
<?php add_action( 'clean_temp_data', 'cleanTempDataCallback' ); function cleanTempDataCallback() { global $wpdb; $wpdb->query( "DELETE FROM {$wpdb->prefix}temporal_data WHERE created_at < NOW() - INTERVAL 1 DAY" ); }
4. Cancel or check a scheduled action
<?php $timestamp = as_next_scheduled_action( 'clean_temp_data' ); if ( $timestamp ) { as_unschedule_action( 'clean_temp_data' ); }
Best practices
- Use
as_schedule_single_action
for one-time tasks andas_schedule_recurring_action
for repeating jobs. - Always check if
as_schedule_*
functions exist before calling them. - If you’re using WP-CLI, you can run tasks manually with:
wp action-scheduler run
Scheduled Actions are a powerful and reliable tool for managing background tasks in WooCommerce. They’re more dependable than WP_Cron
, support automatic retries, and scale well on high-traffic sites. If you’re building features that require background execution, this is the recommended approach.