Last active
January 13, 2024 13:16
-
-
Save curtismchale/4face7b92f5fcab140267e65c3540528 to your computer and use it in GitHub Desktop.
[Add Team AutomateWoo Trigger] see blog post: https://www.nexcess.net/blog/adding-custom-triggers-to-automatewoo/
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; | |
} | |
class Nexcess_Add_To_Team_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 = __( 'User Added to Team', 'automatewoo-custom' ); | |
$this->group = __( 'Teams', '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( 'wc_memberships_for_teams_add_team_member', array( $this, 'catch_hooks' ) ); | |
} | |
/** | |
* Catches the action and calls the maybe_run() method. | |
* | |
* @param $user_id | |
*/ | |
public function catch_hooks( $member ) { | |
$user = $member->get_user(); | |
$user_id = $user->ID; | |
// get/create customer object from the user id | |
$customer = AutomateWoo\Customer_Factory::get_by_user_id( absint( $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; | |
} | |
} // Nexcess_Add_To_Team_Trigger |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment