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
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:
$arg
and order objects are correct$order->get_status();
this line does nothingI 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.
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.