To skip checkout for free packages/products use PHP snippet below.
// Skip checkout for free products and automatically complete the order
add_action('template_redirect', 'skip_checkout_for_free_products');
function skip_checkout_for_free_products() {
// Only proceed if we're on the cart or checkout page, and not the order-received page
if ((is_cart() || is_checkout()) && !is_wc_endpoint_url('order-received')) {
// Get the cart total
$cart_total = WC()->cart->get_total('edit');
// Check if the cart contains only free products (total is 0)
if ($cart_total == 0) {
// Create an order with free products
$order = wc_create_order();
// Add the products in the cart to the order
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$order->add_product($cart_item['data'], $cart_item['quantity']);
}
// Calculate the order totals
$order->calculate_totals();
// Mark the order as completed
$order->set_status('completed');
$order->save();
// Empty the cart
WC()->cart->empty_cart();
// Redirect to the order received page (thank you page)
wp_redirect($order->get_checkout_order_received_url());
exit;
}
}
}
// Automatically mark orders containing only free products as "completed"
add_action('woocommerce_checkout_order_processed', 'auto_complete_order_for_free_products', 10, 1);
function auto_complete_order_for_free_products($order_id) {
// Get the order object
$order = wc_get_order($order_id);
// Flag to check if all items are free
$all_items_free = true;
// Loop through each item in the order
foreach ($order->get_items() as $item) {
if ($item->get_total() > 0) {
// If any item has a price greater than 0, set the flag to false
$all_items_free = false;
break;
}
}
// If all products are free, mark the order as completed
if ($all_items_free) {
$order->update_status('completed');
}
}