Example Custom Trigger for AutomateWoo

Last Updated on: 6th July 2023, 07:12 pm

AutomateWoo Custom Trigger

I recently played around with how to add a custom trigger to AutomateWoo and after trying three outdated examples, this is the best source I could find: https://github.com/curtismchale/nexcess-automatewoo-triggers/

When customer address is updated trigger

I put together a quick working example based on the above repo.

The AutomateWoo Billing Trigger is a custom plugin that adds a new trigger when a customer updates their address. It’s a simple yet effective tool that can be utilized in various scenarios, such as triggering a welcome email when a customer changes their address or notifying the logistics team about the change.

Here’s a brief overview of the plugin’s structure:

  1. The Con_Automatewoo_Triggers class: This class is the plugin’s backbone, initializing the constants and including necessary files. It also adds the new trigger to AutomateWoo.
  2. The customer_updated_address_trigger function: This function adds the custom trigger ‘customer_updated_address’ to AutomateWoo’s triggers.
  3. The Customer_Updated_Address_Trigger class: This class extends AutomateWoo’s Trigger class. It defines what data will be supplied to the workflow (in this case, ‘customer’) and registers the necessary hooks.

View the code

class-custom-trigger.php

<?php
/*
Plugin Name: AutomateWoo Billing Trigger
Description: Adds custom triggers to AutomateWoo
Version: 1.0
Author: Your Name
Author URI: Your Website
License: GPLv2 or later
*/

class Con_Automatewoo_Triggers{

    private static $instance;

    public static function instance(){

        if ( ! self::$instance ){
            self::$instance = new Con_Automatewoo_Triggers();
            self::$instance->init();
        }

    } // instance

    public function init(){

        $this->constants();
        $this->includes();

        add_filter( 'automatewoo/triggers', array( $this, 'customer_updated_address_trigger' ), 10, 1 );

    } // init

    public static function customer_updated_address_trigger( $triggers ){

        require_once( 'trigger-customer-updated-address.php' );

        $triggers['customer_updated_address'] = 'Customer_Updated_Address_Trigger';

        return $triggers;

    }

    public function constants(){

        define( 'CON_AUTOMATEWOO_TRIGGERS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );

    }

    public function includes(){
    }

} // Con_Automatewoo_Triggers

Con_Automatewoo_Triggers::instance();

trigger-customer-updated-address.php

<?php

if ( ! defined( 'ABSPATH' ) ){
    exit;
}

class Customer_Updated_Address_Trigger extends AutomateWoo\Trigger{

    public $supplied_data_items = array( 'customer' );

    public function init() {
        $this->title = __( 'Customer Updated Address', 'text-domain' );
        $this->group = __( 'Custom Triggers', 'text-domain' );
    }

    public function load_fields() {}

    public function register_hooks() {
        add_action( 'woocommerce_customer_save_address', array( $this, 'catch_hooks' ), 10, 2 );
    }

    public function catch_hooks( $user_id, $load_address ) {
        $customer = AutomateWoo\Customer_Factory::get_by_user_id( $user_id );
            
        $this->maybe_run(array(
            'customer' => $customer,
        ));
    }

    public function validate_workflow( $workflow ) {
        $customer = $workflow->data_layer()->get_customer();

        // do something...

        return true;
    }

} // Customer_Updated_Address_Trigger

Download

Leave a Reply

Your email address will not be published. Required fields are marked *

Con Schneider