Automatewoo provides powerful marketing automation for your woocommerce store.  You can achieve automation by creating various workflow.  This plugin works fine on your WooCommerce subscriptions, WoooCommerce Vendor and membership plugin as well.

Workflow comes in action with triggers, rules, Variables and action. Please visit this link for more details.

On this article, we are going to develop custom trigger which you can add on your workflow. This article takes a reference from this Automatewoo documentation and just add a few more information. Just for information, Automatewoo works under the hood of hooks available on your plugins[ woocommerce, subscriptions. vendor plugins…].

This code add these following extra information:

  • Sending custom arguments inside validate_workflow() methods.
  • Adding custom validation for the data(Hooks: automatewoo_validate_data_item)
  • Pull custom args from the data layer( $workflow->data_layer()->get_item( ‘custom_args’ ); )

Please go through the comments added on the following code to have more insight of methods.

<?php /**  * 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 { 	/** @var array - Define which data items are set by this trigger, this determines which rules and actions will be available */ 	public $supplied_data_items = array( 'customer' ); 	/** 	 * Set up the trigger 	 */ 	public function init() { 		$this->title = __( 'My Custom Trigger', 'your-slug' );
		$this->group = __( 'Custom Triggers', 'your-slug' );
	}


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


	/**
	 * Defines action when the hooks my_custom_action run.
	 */
	public function register_hooks() {
		/**
		* Add data item on data-layer
		* @hooked automatewoo_validate_data_item
		* @see add_custom_data_item_on_data_layer
		*/
		add_filter( 'automatewoo_validate_data_item', array( $this, 'add_custom_data_item_on_data_layer' ), 10, 3 );

		add_action( 'my_custom_action', array( $this, 'your_desired_action' ), 10, 2 );
	}

	/**
	* @param bool $valid
	* @param string $type Array Index
	* @param mixed $item
	* @return bool
	*/
	public function add_custom_data_item_on_data_layer( $valid, $type, $item  ) {

		if( 'custom_args' === $type ) {
			$valid = true;
		} 
		return $valid;
	}


	/**
	 * Catches the action and calls the maybe_run() method.
	 * 
	 * @param $user_id
	 */
	public function your_desired_action( $user_id, $custom_args ) {
		
		// get/create customer object from the user id
		$customer = AutomateWoo\Customer_Factory::get_by_user_id( $user_id );
		/**
		* THis methods provides necessary date to run the trigger.
		* You can send the arguments like subscriptions, order, customer in the form of array.
		*
		* If you want to send some custom arguments, then you need to add validation for that as well.
		*/	
		$this->maybe_run(array(
			'customer' => $customer,
			'custom_args' => $custom_args
		));
	}


	/**
	 * Performs any validation if required. If this method returns true the trigger will fire.
	 * This methods run inside the maybe_run methods to validate the trigger. You can stopped your trigger to run on certain condition with the help of this function.
	 * @param $workflow AutomateWoo\Workflow
	 * @return bool
	 */
	public function validate_workflow( $workflow ) {

		// Get objects from the data layer
		$customer = $workflow->data_layer()->get_customer();
		/**
		*  $workflow->data_layer()->get_item() will only works if data inside the data layer  object is validation,
		* By default custom args passed will be invalidated
		*/
		$custom_args_value = $workflow->data_layer()->get_item( 'custom_args' );

		return true;
	}

}


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;
}

Note: Hope this article may help you with creating the custom trigger as per your requirements.

Thanks

Advertisements

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: