Custom Triggers

Note:

This is a Developer level doc. If you are unfamiliar working with code and resolving potential conflicts, we recommend you work with a Woo Agency Partner for larger projects, or find a WooCommerce developer on Codeable for smaller customizations. We are unable to provide support for customizations under our Support Policy.

Like most WordPress plugins, AutomateWoo can be extended by other developers to add functionality. AutomateWoo triggers are complex by nature, and creating custom triggers is considerably more difficult than creating a custom function which can be easily tied to an action.

Using AutomateWoo async events

↑ Back to top

In version 4.8, AutomateWoo optimized it’s async events system, which has implications for how async triggers function. If you’re developing custom triggers, we recommend having a look at the asynchronous events documentation. This is especially important if you are hooking your custom trigger to any of AutomateWoo’s async actions.

Custom trigger example

↑ Back to top

The snippets below are an example of a custom trigger that is triggered by a WordPress action hook. This could be initiated anywhere in your code by using WordPress’ do_action() function. The action needs a single User ID which is then caught by the custom trigger, which in turn passes the user along to any workflows which are set up to use the custom trigger.

First create a new file named class-custom-trigger.php in your theme and add the below example trigger. You can modify this as needed to rename it, add to another trigger group, define when the trigger runs, and ultimately what it does when triggered.

<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Prevent direct access
}

/**
 * This is an example trigger that is triggered via a WordPress action and includes a user data item.
 * Trigger with: do_action('my_custom_action', $user_id );
 */
class My_AutomateWoo_Custom_Trigger extends AutomateWoo\Trigger {

	/**
	 * Define which data items are set by this trigger, this determines which rules and actions will be available
	 *
	 * @var array
	 */
	public $supplied_data_items = array( 'customer' );

	/**
	 * Set up the trigger
	 */
	public function init() {
		$this->title = __( 'My Custom Trigger', 'automatewoo-custom' );
		$this->group = __( 'Custom Triggers', 'automatewoo-custom' );
	}

	/**
	 * Add any fields to the trigger (optional)
	 */
	public function load_fields() {}

	/**
	 * Defines when the trigger is run
	 */
	public function register_hooks() {
		add_action( 'my_custom_action', array( $this, 'catch_hooks' ) );
	}

	/**
	 * Catches the action and calls the maybe_run() method.
	 *
	 * @param $user_id
	 */
	public function catch_hooks( $user_id ) {
		
		// get/create customer object from the user id
		$customer = AutomateWoo\Customer_Factory::get_by_user_id( $user_id );
			
		$this->maybe_run(array(
			'customer' => $customer,
		));
	}

	/**
	 * Performs any validation if required. If this method returns true the trigger will fire.
	 *
	 * @param $workflow AutomateWoo\Workflow
	 * @return bool
	 */
	public function validate_workflow( $workflow ) {

		// Get objects from the data layer
		$customer = $workflow->data_layer()->get_customer();

		// do something...

		return true;
	}

}

After saving that file in your theme, put the below code in your theme’s functions.php file.

<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Prevent direct access
}

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

/**
 * @param array $triggers
 * @return array
 */
function my_custom_triggers( $triggers ) {

	include_once 'class-custom-trigger.php';

	// set a unique name for the trigger and then the class name
	$triggers['my_custom_trigger'] = 'My_AutomateWoo_Custom_Trigger';

	return $triggers;
}
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.