add_action('woocommerce_thankyou', 'wpp_auto_complete_free_fully_downloadable_orders');
function wpp_auto_complete_free_fully_downloadable_orders($order_id) {
$order = wc_get_order($order_id);
if (!$order || !in_array($order->get_status(), ['pending', 'on-hold', 'processing'])) {
return;
}
if ($order->get_total() != 0) {
return;
}
$all_downloadable = true;
foreach ($order->get_items() as $item) {
$product = $item->get_product();
if (!$product || !$product->is_downloadable()) {
$all_downloadable = false;
break;
}
}
if ($all_downloadable) {
$order->update_status(
'completed',
'All items are free and downloadable. Order completed automatically.'
);
}
}