skip to Main Content

In the application I am running, we need to stop certain orders from getting sent to inFlow inventory management.

I want to stop orders with the status of Estimate from making it all the way through the process.

From what I’ve read, hooking into woocommerce_webhook_should_deliver and returning $should_delever == false should stop the webhook from firing, but I am unable to get it to.

function should_deliver_order_creation($should_deliver, $wc_webhook, $arg) {
    $order = wc_get_order($arg);
    $order->get_status();
    if(str_contains($wc_webhook->get_name(), 'inFlow') && $order->get_status() == 'estimate') {
        $should_deliver = false;
    }
    return $should_deliver;
}

add_filter('woocommerce_webhook_should_deliver', 'should_deliver_order_creation', 9, 3);

Orders with a status of ‘estimate’ are still going through

Are there any other hooks I should try to get into to to make the order stop going through?

2

Answers


  1. woocommerce_webhook_should_deliver is the correct and I think only way to accomplish this.

    Here are a few steps that could validate your code for you:

    1. You should check if your code is able to get the order object correctly, you can do a debug and die to see if your $arg and order objects are correct
    2. $order->get_status(); this line does nothing
    3. Instead of comparing the name of the webhook I would recommend checking on the delivery url, because anybody could change the name of the hook, while the url has to be a target to the inFlow infrastructure
    4. Check the order status is correct, it seems to be a non default one, see those defaults for their names, just make sure yours is correct

    I was able to use the hook for a slightly different purpose, but also to stop delivering some webhooks, and get it working, perhaps this code helps you as well.

    Login or Signup to reply.
  2. When the webhook is fired through an order.created trigger, the order object is empty, so getting it’s status with $order->get_status() results in an empty string. I’m having the same issue when trying to fetch the order items using $order->get_items() to determine if the webhook should be delivered or not.

    When the webhook is fired through an order.updated trigger, the order objects is fully populated and contains the correct status and order items.

    In my situation, I am able to get the order items from the cart object instead of the order object: wc()->cart->get_cart(), but I’m not sure how to retrieve the order status of an order that hasn’t been fully stored yet.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search