We have some 'action' type webhooks setup, but sometimes we want to resend the data originally sent via webhook. For instance the webhook starts an automation process, but failed, we need to start ti again. To do this I added a custom order actions to the admin meta box, but now I need them to actually (re)fire the webhook.
function pwk_wc_add_order_meta_box_action( $actions ) {
global $theorder;
// continue if the order completed, processing or refunded
if ( $theorder->has_status('processing') || $theorder->has_status('completed') || $theorder->has_status('refunded') ) {
$actions['wc_resend_order_webhook'] = __( 'Refire this order webhook', 'text-domain' );
return $actions;
} else {
return $actions;
}
}
My webhook is setup like this:
Name | action.woocommerce_payment_complete |
---|---|
Status | Active |
Topic | Action |
Action event | woocommerce_payment_complete |
What action can I call to run a webhook? I don't want the order emails to be sent only the webhook to fire. In my situation it's the "woocommerce_payment_complete" but really how does one refire a webhook for any action or topic?
EDIT 1: So I created a new webhook with the Action event woocommerce_order_action_wc_resend_order_webhook
, and then added something to the function:
function pwk_process_resend_order_webhook_meta_box_action( $order ) {
// add the order note
$message = sprintf( __( 'Order resent to integromat.', 'text-domain' ));
$order->add_order_note( $message );
$arg = $order->get_order_number();
return $arg;
}
add_action( 'woocommerce_order_action_wc_resend_order_webhook', 'pwk_process_resend_order_webhook_meta_box_action' );
Although this does fire a webhook, it looks like it's an array not a simple order id:
{
"action": "woocommerce_order_action_wc_resend_order_webhook",
"arg": []
}
A solution was to exploit the woocommerce_order_action as it allows new actions, then running a do_action
which creates a action with the sole purpose of firing the webhook. The action name woocommerce_order_action_fire_webhook_order
is made up
i.e. <woocommerce_order_action_>anything_you_want
works too
function pwk_process_resend_order_webhook_meta_box_action( $order ) {
$message = sprintf( __( 'Order details resent via webhook.', 'text-domain' ));
$order->add_order_note( $message );
do_action( 'woocommerce_order_action_fire_webhook_order', $order->get_order_number() );
}
add_action( 'woocommerce_order_action_wc_resend_order_webhook', 'pwk_process_resend_order_webhook_meta_box_action' );
Then also create a webhook with the action
type for the event woocommerce_order_action_fire_webhook_order
tbh I'm not entirely sure why this works, so any comments or edits are most welcome.