Skip to content

Instantly share code, notes, and snippets.

@danielbitzer
Last active January 22, 2023 21:39
Show Gist options
  • Save danielbitzer/92a39e0f5c426441caa9b909597dfd70 to your computer and use it in GitHub Desktop.
Save danielbitzer/92a39e0f5c426441caa9b909597dfd70 to your computer and use it in GitHub Desktop.
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 ),
)
);
}
}
@samirrifai
Copy link

This looks fairly easy, but I’m struggling because I’m not a coder. Where should we create the php custom trigger file? Inside the child theme I suppose? I’m using Woocommerce Order Status Manager and would like to add custom triggers for sending emails using Automatewoo.
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment