Skip to content

Instantly share code, notes, and snippets.

@danielbitzer
Last active August 26, 2024 15:25
Show Gist options
  • Save danielbitzer/fb93e54cf430ecb9f319 to your computer and use it in GitHub Desktop.
Save danielbitzer/fb93e54cf430ecb9f319 to your computer and use it in GitHub Desktop.
AutomateWoo Custom Trigger Example
<?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;
}
@t3ch9
Copy link

t3ch9 commented Feb 4, 2019

Hello, I've implemented the code above but Wordpress crashes. I see that this code is from 2 years ago. Is it supposed to work with the current Automatewoo version?

@aishamerhebi
Copy link

did it work for you? where did you add those classes?

Copy link

ghost commented Oct 21, 2021

where should i put this code in my wordpress?

@samirrifai
Copy link

How to add a trigger to AutomateWoo for a custom order status?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment