Skip to content

Instantly share code, notes, and snippets.

@crstauf
Last active October 20, 2017 19:31
Show Gist options
  • Save crstauf/151018d5bed097e50955a8d5a32a2f63 to your computer and use it in GitHub Desktop.
Save crstauf/151018d5bed097e50955a8d5a32a2f63 to your computer and use it in GitHub Desktop.
add_action( 'woocommerce_webhook_delivery', function( $args, $response, $duration, $arg, $webhook_id ) {
$order = wc_get_order( $arg );
if (
empty( $order )
|| is_wp_error( $order )
|| !is_a( $order, 'WC_Order' )
)
return;
$webhook = new WC_Webhook( $webhook_id );
if (
empty( $webhook )
|| is_wp_error( $webhook )
|| !is_a( $webhook, 'WC_Webhook' )
)
return;
if ( is_wp_error( $response ) ) {
$errors_output = array();
foreach ( $response->errors as $type => $errors )
foreach ( $errors as $error )
$errors_output[] = $type . ': ' . $error;
$order->add_order_note(
'Webhook <code style="font-size: 0.8em;">' . $webhook->get_name() . '</code> fired, and failed:' . "\n" .
'<ul><li>' . implode( '</li><li>', $errors_output ) . '</li></ul>' .
'Connection duration: ' . $duration . 's'
);
} else
$order->add_order_note(
'Webhook <code style="font-size: 0.8em;">' . $webhook->get_name() . '</code> fired.' . "\n" .
'Response code: ' . $response['response']['code'] . "\n" .
'Connection duration: ' . $duration . 's'
);
}, 10, 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment