Last active
August 26, 2024 15:25
-
-
Save danielbitzer/fb93e54cf430ecb9f319 to your computer and use it in GitHub Desktop.
AutomateWoo Custom Trigger Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
where should i put this code in my wordpress?
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
did it work for you? where did you add those classes?