Automating WooCommerce: How to Add a Job to the Action Scheduler

WooCommerce Action Scheduler is a powerful tool that allows you to schedule and automate tasks within your WooCommerce store. Whether you need to send follow-up emails, update inventory, or perform any other repetitive tasks, the Action Scheduler can handle it efficiently. In this blog post, we’ll explore how to add a job to the WooCommerce Action Scheduler with practical PHP code examples.

What is the WooCommerce Action Scheduler?

The Action Scheduler is a background processing library that WooCommerce uses to manage scheduled tasks. It provides a robust and flexible way to schedule one-time or recurring tasks, ensuring they are executed at the right time without impacting your store’s performance.

Installing the Action Scheduler

Before we start, ensure that the Action Scheduler is installed and active. It is included with WooCommerce by default, but if you’re using it outside WooCommerce, you can install it via Composer:

composer require automattic/action-scheduler

Adding a Job to the Action Scheduler

To add a job to the WooCommerce Action Scheduler, follow these steps:

  1. Create the Action Hook
  2. Schedule the Action
  3. Handle the Scheduled Action

Let’s go through each step with code examples.

Step 1: Create the Action Hook

First, define the action hook that will be triggered by the scheduler. This hook is a custom function that performs the task you want to automate.

// Define the action hook
add_action('my_custom_action_hook', 'my_custom_action_function');

/**
 * Function to be executed when the action is triggered.
 */
function my_custom_action_function($arg1, $arg2) {
    // Your custom task logic here
    // For example, sending an email
    wp_mail($arg1, 'Subject', 'Message content: ' . $arg2);
}

Step 2: Schedule the Action

Next, schedule the action using one of the Action Scheduler’s scheduling functions. You can schedule a one-time action or a recurring action. Here, we’ll show how to schedule both.

One-time Action:

// Schedule a one-time action
if (!as_next_scheduled_action('my_custom_action_hook', array('[email protected]', 'Hello'))) {
    as_schedule_single_action(strtotime('+1 hour'), 'my_custom_action_hook', array('[email protected]', 'Hello'));
}

Recurring Action:

// Schedule a recurring action
if (!as_next_scheduled_action('my_custom_action_hook', array('[email protected]', 'Recurring Hello'))) {
    as_schedule_recurring_action(time(), 3600, 'my_custom_action_hook', array('[email protected]', 'Recurring Hello')); // Runs every hour
}

Step 3: Handle the Scheduled Action

The function my_custom_action_function will be called at the scheduled time with the provided arguments. You can customize this function to perform any task you need.

Complete Example

Here’s a complete example that schedules a one-time email to be sent one hour from now:

// Add the action hook
add_action('send_scheduled_email', 'send_scheduled_email_function');

/**
 * Function to send an email.
 *
 * @param string $email The recipient email address.
 * @param string $message The email message.
 */
function send_scheduled_email_function($email, $message) {
    // Send the email
    wp_mail($email, 'Scheduled Email', $message);
}

// Schedule the email to be sent one hour from now
if (!as_next_scheduled_action('send_scheduled_email', array('[email protected]', 'This is a scheduled email'))) {
    as_schedule_single_action(strtotime('+1 hour'), 'send_scheduled_email', array('[email protected]', 'This is a scheduled email'));
}

Conclusion

By leveraging the WooCommerce Action Scheduler, you can automate repetitive tasks and improve the efficiency of your WooCommerce store. This guide provided you with the necessary steps and code examples to add a job to the Action Scheduler, ensuring your tasks are handled seamlessly in the background. Start implementing automation in your store today and experience the benefits of a more efficient e-commerce operation.

Get A No Obligation Quote

Do You Need Help With Your WooCommerce Site?

Click through to the next page and complete the form to get a free no obligation quote to fix any issue you are having with your WooCommerce site.