Skip to content

Instantly share code, notes, and snippets.

@danielbitzer
Last active Nov 22, 2021
Embed
What would you like to do?
AutomateWoo - Async custom trigger example
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Register AutomateWoo triggers.
add_filter( 'automatewoo/triggers', function ( $triggers ) {
// Include the file containing the trigger class
require_once 'my-custom-order-paid-trigger.php';
// Add the trigger to the $triggers array
// Set a unique name for the trigger and then the class name
$triggers['my_custom_order_paid_trigger'] = 'My_Custom_Order_Paid_Trigger';
return $triggers;
} );
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class My_Custom_Order_Paid_Trigger
*/
class My_Custom_Order_Paid_Trigger extends \AutomateWoo\Trigger {
/**
* Async events required by the trigger.
*
* Since we are using the `automatewoo/order/paid_async` action we must require the `order_paid` event.
*
* @var string|array
*/
protected $required_async_events = 'order_paid';
/**
* Load trigger admin details.
*/
public function load_admin_details() {
$this->title = __( 'Order Paid Custom Version', 'automatewoo' );
$this->description = __( 'This is trigger uses the async order paid event.', 'automatewoo' );
}
/**
* Register trigger's hooks.
*/
public function register_hooks() {
add_action( 'automatewoo/order/paid_async', [ $this, 'handle_trigger_event' ] );
}
/**
* Callback for async order paid trigger event.
*
* @param int $order_id
*/
public function handle_trigger_event( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
}
// Attempt to run any workflows that are using this trigger.
$this->maybe_run(
array(
'order' => $order,
'customer' => \AutomateWoo\Customer_Factory::get_by_order( $order ),
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment