Create a trigger from the asynchronous order-paid event

This two-file plugin example registers a trigger that listens to AutomateWoo’s asynchronous order-paid event. Requiring order_paid ensures AutomateWoo schedules that event.

Developer note:

Add custom PHP in a small custom plugin or child theme, and test it on a staging site before production. This example requires ongoing maintenance when extension APIs change.

plugin.php

<?php

defined( 'ABSPATH' ) || exit;

add_filter( 'automatewoo/triggers', 'my_store_register_order_paid_trigger' );

/**
 * Register the custom order-paid trigger.
 *
 * @param array $triggers Registered triggers.
 * @return array
 */
function my_store_register_order_paid_trigger( $triggers ) {
	require_once __DIR__ . '/class-my-store-order-paid-trigger.php';

	$triggers['my_store_order_paid'] = 'My_Store_Order_Paid_Trigger';

	return $triggers;
}

class-my-store-order-paid-trigger.php

<?php

defined( 'ABSPATH' ) || exit;

/**
 * Custom asynchronous order-paid trigger.
 */
class My_Store_Order_Paid_Trigger extends AutomateWoo\Trigger {
	/**
	 * Required asynchronous events.
	 *
	 * @var string|array
	 */
	protected $required_async_events = 'order_paid';

	/**
	 * Load trigger details.
	 */
	public function load_admin_details() {
		$this->title       = __( 'Order paid: custom', 'my-store' ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
		$this->description = __( 'Runs after the asynchronous order-paid event.', 'my-store' ); // phpcs:ignore WordPress.WP.I18n.TextDomainMismatch
	}

	/**
	 * Register trigger hooks.
	 */
	public function register_hooks() {
		add_action( 'automatewoo/order/paid_async', array( $this, 'handle_event' ) );
	}

	/**
	 * Handle the asynchronous event.
	 *
	 * @param int $order_id Order ID.
	 */
	public function handle_event( $order_id ) {
		$order = wc_get_order( $order_id );

		if ( ! $order ) {
			return;
		}

		$this->maybe_run(
			array(
				'order'    => $order,
				'customer' => AutomateWoo\Customer_Factory::get_by_order( $order ),
			)
		);
	}
}
Use of your personal data
We and our partners process your personal data (such as browsing data, IP Addresses, cookie information, and other unique identifiers) based on your consent and/or our legitimate interest to optimize our website, marketing activities, and your user experience.