1. Documentation /
  2. AutomateWoo /
  3. Asynchronous events

Asynchronous events

AutomateWoo processes many triggers asynchronously by using asynchronous events. Such events are separated from the main event and slightly delayed.

Prior to AutomateWoo 4.8, these async events would always be temporarily stored in the database and run in the background. This happened regardless of whether they were being used or not.

AutomateWoo 4.8 introduces a number of optimizations to how the async events system works. The main difference is that async events will now only run if the site has an active workflow that requires the event.

Let’s say your site isn’t using the Customer Account Created trigger. In that case, AutomateWoo will no longer create an async event when a customer creates an account.

Implications for custom triggers

↑ Back to top

Each workflow’s required async events are set by the workflow’s trigger. This means that custom triggers must declare which async events they require if they are triggered by an async event hook.

One example of this is the Order Paid trigger which is initiated by the WordPress action automatewoo/order/paid_async. This is an async event. It is fired asynchronously by being stored in the database and run shortly after the original order paid event happens. Therefore the Order Paid trigger must declare that it requires the order_paid async event.

An async event requirement is declared on a trigger with the $required_async_events property as shown in the code example below.

How do I know if my trigger requires an async event?

↑ Back to top

If your trigger uses any of the async action hooks that are listed below you will need to add the event name to the $required_async_events property of that trigger.

List of async events

↑ Back to top

The following is a list of async event names with their corresponding WordPress actions.

  • order_created : automatewoo/async/order_created
  • order_status_changed: automatewoo/order/status_changed_async
  • order_paid: automatewoo/order/paid_async
  • order_pending: automatewoo_order_pending
  • review_approved: automatewoo/review/posted_async
  • user_registered: automatewoo/async/user_registered
  • subscription_created: automatewoo/async/subscription_created
  • subscription_status_changed: automatewoo/subscription/status_changed_async
  • subscription_renewal_payment_complete: automatewoo/subscription/renewal_payment_complete_async
  • subscription_renewal_payment_failed: automatewoo/subscription/renewal_payment_failed_async
  • membership_status_changed: automatewoo/membership_status_changed_async
  • mc4wp_form_success: automatewoo/mc4wp_form_success_async

Custom trigger example using an existing async event

↑ Back to top
<?php
     
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

// Register AutomateWoo triggers.
add_filter( 'automatewoo/triggers', function ( $triggers ) {
	// Include the file containing the trigger class
	require_once 'my-custom-order-paid-trigger.php';

	// Add the trigger to the $triggers array
	// Set a unique name for the trigger and then the class name
	$triggers['my_custom_order_paid_trigger'] = 'My_Custom_Order_Paid_Trigger';

	return $triggers;
} );

Code Snippet on Github

<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class My_Custom_Order_Paid_Trigger
 */
class My_Custom_Order_Paid_Trigger extends \AutomateWoo\Trigger {

	/**
	 * Async events required by the trigger.
	 *
	 * Since we are using the `automatewoo/order/paid_async` action we must require the `order_paid` event.
	 *
	 * @var string|array
	 */
	protected $required_async_events = 'order_paid';

	/**
	 * Load trigger admin details.
	 */
	public function load_admin_details() {
		$this->title       = __( 'Order Paid Custom Version', 'automatewoo' );
		$this->description = __( 'This is trigger uses the async order paid event.', 'automatewoo' );
	}

	/**
	 * Register trigger's hooks.
	 */
	public function register_hooks() {
		add_action( 'automatewoo/order/paid_async', [ $this, 'handle_trigger_event' ] );
	}

	/**
	 * Callback for async order paid trigger event.
	 *
	 * @param int $order_id
	 */
	public function handle_trigger_event( $order_id ) {
		$order = wc_get_order( $order_id );

		if ( ! $order ) {
			return;
		}

		// Attempt to run any workflows that are using this trigger.
		$this->maybe_run(
			array(
				'order'    => $order,
				'customer' => \AutomateWoo\Customer_Factory::get_by_order( $order ),
			)
		);
	}

}

Code snippet on GitHub.