Hello guys I have been puzzled by this situation for two days now and I can not discover a option to make it working. I’ve WooCommerce webhook triggered when new Order is created, however I need to forestall execution for orders which gadgets are in sure classes. I am making an attempt to attain this by utilizing the woocommerce_webhook_should_deliver filter-hook. Right here is my try and I will clarify the difficulty with it:
perform custom_woocommerce_webhook_should_deliver($shouldDeliver, $occasion, $arg) {
$excludeCategories = [16, 17];
$order = wc_get_order($arg);
$logger = wc_get_logger();
foreach ($order->get_items() as $item_id => $merchandise) {
$term_ids = wp_get_post_terms(
$item->get_product_id(), 'product_cat', array('fields' => 'ids')
);
$consequence = array_diff($excludeCategories, $term_ids);
$logger->add(
sprintf(
"send-order-debug-%s",
(int)$arg
), json_encode(['res' => $result, 'ids' => $term_ids])
);
if (depend($excludeCategories) !== depend($consequence)) {
$shouldDeliver = false;
break;
}
}
$logger->add(
sprintf(
"send-order-debug-%s",
(int)$arg
),
json_encode(
[
'shouldDeliver' => (int)$shouldDeliver,
'itemsCount' => count($order->get_items()),
]
)
);
return $shouldDeliver;
}
add_filter('woocommerce_webhook_should_deliver', 'custom_woocommerce_webhook_should_deliver', 10, 3);
As you may see what I am doing is to get the order and attempt to forestall the webhook execution if there are gadgets in 2 classes(hardcoded within the instance). The issue and for my massive shock is that within the second of this examine $order->get_items() returns empty array, thus my filter at all times returns true. Right here is the log I’ve put on the finish of the tactic.
{"shouldDeliver":1,"itemsCount":0}
The attention-grabbing factor right here to know is that there’s nothing mistaken with the order content material, it is not empty and when webhook is executed I obtain the order with it is gadgets.
I am not a WooCommerce/WordPress guru and I do not know what’s the method of saving orders and the order gadgets, however I assume that within the second I get on this filter, order gadgets are nonetheless not created. Anybody can provide me some concepts of the way to make this working? I am open to completely totally different strategy of fixing this job(please with out nugatory assumptions, solely examined and dealing examples). Thanks upfront!