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 topIn 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 topThis is an example of a custom trigger than is triggered by a WordPress action hook. This could be initiated anywhere in your code by using WordPress’s 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.
![aw-custom-trigger](https://woocommerce.com/wp-content/uploads/2016/01/aw-custom-trigger.png?w=1024&h=459)
<?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; | |
} | |
} |
<?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; | |
} |