admin管理员组

文章数量:1334133

I am trying to implement webhook retry with woocommerce. I see where they publish the webhook notification at .php#L353, but there is no reference to $this. Is it possible to get a webhook object by id instead? Then I could keep it in memory and retry after some delay and/or put something in db for retry, etc.

This should all be built in to be honest, but I want to be able to handle server blips, etc. and ensure that my subscription web hooks are eventually fulfilled without manual intervention.

EDIT

I've come up with this code, I would love any feedback on it:

<?php

/*
Plugin Name: Custom Stuff
Description: Custom Functions, etc.
*/

// make sure woocommerce never disables a webhook
function overrule_webhook_disable_limit($number)
{
    return 999999999999; //very high number hopefully you'll never reach.
}
add_filter('woocommerce_max_webhook_delivery_failures', 'overrule_webhook_disable_limit');

// woocommerce webhook retry queue
function woocommerce_webhook_retry($id, $arg)
{
        do_action('woocommerce_deliver_webhook_async', $id, $arg);
}

// add listener for woocommerce web hooks
function woocommerce_webhook_listener_custom($http_args, $response, $duration, $arg, $id)
{
        $responseCode = wp_remote_retrieve_response_code($response);
        if ($responseCode < 200 || $responseCode > 299)
        {
                // re-queue web-hook for another attempt
                $timestamp = new DateTime('+1 minutes');
                WC()->queue()->schedule_single($timestamp, 'woocommerce_webhook_retry', $args = array($id, $arg), $group = 'wc_webhook_retry');
        }
}

add_action('woocommerce_webhook_delivery', 'woocommerce_webhook_listener_custom', 10, 5);

I am trying to implement webhook retry with woocommerce. I see where they publish the webhook notification at https://github/woocommerce/woocommerce/blob/5ba2cdafa58c5a02d44eebbab1e8fe26f295e4aa/includes/class-wc-webhook.php#L353, but there is no reference to $this. Is it possible to get a webhook object by id instead? Then I could keep it in memory and retry after some delay and/or put something in db for retry, etc.

This should all be built in to be honest, but I want to be able to handle server blips, etc. and ensure that my subscription web hooks are eventually fulfilled without manual intervention.

EDIT

I've come up with this code, I would love any feedback on it:

<?php

/*
Plugin Name: Custom Stuff
Description: Custom Functions, etc.
*/

// make sure woocommerce never disables a webhook
function overrule_webhook_disable_limit($number)
{
    return 999999999999; //very high number hopefully you'll never reach.
}
add_filter('woocommerce_max_webhook_delivery_failures', 'overrule_webhook_disable_limit');

// woocommerce webhook retry queue
function woocommerce_webhook_retry($id, $arg)
{
        do_action('woocommerce_deliver_webhook_async', $id, $arg);
}

// add listener for woocommerce web hooks
function woocommerce_webhook_listener_custom($http_args, $response, $duration, $arg, $id)
{
        $responseCode = wp_remote_retrieve_response_code($response);
        if ($responseCode < 200 || $responseCode > 299)
        {
                // re-queue web-hook for another attempt
                $timestamp = new DateTime('+1 minutes');
                WC()->queue()->schedule_single($timestamp, 'woocommerce_webhook_retry', $args = array($id, $arg), $group = 'wc_webhook_retry');
        }
}

add_action('woocommerce_webhook_delivery', 'woocommerce_webhook_listener_custom', 10, 5);
Share Improve this question edited Jul 9, 2020 at 15:40 jjxtra asked Jul 8, 2020 at 23:57 jjxtrajjxtra 1416 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

This seems to do the trick, it retries every 5 minutes and uses the built in woocommerce action name.

<?php

/*
Plugin Name: Custom Stuff
Description: Custom Functions, etc.
*/

// make sure woocommerce never disables a webhook
function overrule_webhook_disable_limit($number)
{
    return 999999999999; //very high number hopefully you'll never reach.
}
add_filter('woocommerce_max_webhook_delivery_failures', 'overrule_webhook_disable_limit');

// add listener for woocommerce web hooks
function woocommerce_webhook_listener_custom($http_args, $response, $duration, $arg, $id)
{
        $responseCode = wp_remote_retrieve_response_code($response);
        if ($responseCode < 200 || $responseCode > 299)
        {
                // re-queue web-hook for another attempt, retry every 5 minutes until success
                $timestamp = new DateTime('+5 minutes');
                $argsArray = array('webhook_id' => $id, 'arg' => $arg);
                WC()->queue()->schedule_single($timestamp, 'woocommerce_deliver_webhook_async', $args = $argsArray, $group = 'woocommerce-webhooks');
        }
}

add_action('woocommerce_webhook_delivery', 'woocommerce_webhook_listener_custom', 10, 5);

本文标签: WooCommerce WebHook Retry