DEV Community

Jeremy Libeskind
Jeremy Libeskind

Posted on

Modernizing Travel Booking Plugins

Modernizing Travel [Booking] Plugins: Building a Reservation & Pricing Calculator in Python

Travel reservation plugins (plugins de réservation de voyage) are the backbone of modern tourism websites. Whether you’re building a WordPress plugin, a custom booking engine, or an internal tool for a travel agency, these systems need to handle:

  • Multi-leg flights and dynamic pricing
  • Hotel availability and nightly rates
  • Car rentals, tours, and activity add-ons
  • Promo codes, seasonal surcharges, and taxes
  • Instant quote generation for customers

Small travel operators and indie developers often rely on spreadsheets or manual math. That’s exactly where a simple Python script can become the heart of a professional reservation plugin.

In this article, I’ll show you a practical, ready-to-use Travel Booking Calculator that simulates the core logic of a real reservation plugin. You can run it locally today and later turn it into a full API or web app.

Why Automation Matters for Travel Booking Plugins

  • Speed: Customers expect instant quotes — not “we’ll email you in 24h”
  • Accuracy: No more miscalculating nights, passenger fees, or currency conversions
  • Flexibility: Easily add new services (tours, transfers, insurance)
  • Professionalism: Clean, branded booking summaries you can email or display on your site
  • Scalability: Handle peak seasons without hiring extra staff

Let’s Build the Tool: Travel Reservation Calculator

Here’s a clean, extensible Python script. It supports flights, hotels, add-ons, promo codes, taxes, and generates a complete reservation summary — just like the backend of a real booking plugin.


python
def calculate_flight_cost(passengers: int, base_fare: float) -> float:
    """Simple flight pricing with per-passenger fees"""
    return base_fare * passengers

def calculate_hotel_cost(nights: int, nightly_rate: float, rooms: int = 1) -> float:
    """Hotel cost based on nights and rooms"""
    return nightly_rate * nights * rooms

def apply_promo(subtotal: float, promo_code: str = None) -> float:
    """Travel-specific promo rules"""
    promos = {
        "FLY15": 0.15,      # 15% off flights
        "STAY20": 0.20,     # 20% off hotels
        "TRAVEL10": 0.10,   # General discount
        "EARLYBIRD": 0.25,  # Early booking special
    }
    discount_rate = promos.get(promo_code.upper() if promo_code else "", 0.0)
    return subtotal * (1 - discount_rate)

def get_taxes_and_fees(subtotal_after_promo: float) -> float:
    """Typical travel taxes + service fees"""
    return subtotal_after_promo * 0.18  # 18% combined VAT + airport + service fees

def generate_travel_reservation(
    passengers: int,
    flight_base_fare: float,
    nights: int,
    nightly_rate: float,
    rooms: int = 1,
    add_ons: dict = None,  # e.g. {"Airport Transfer": 45.0}
    promo_code: str = None
) -> dict:

    flight_cost = calculate_flight_cost(passengers, flight_base_fare)
    hotel_cost = calculate_hotel_cost(nights, nightly_rate, rooms)
    add_on_total = sum(price for price in (add_ons or {}).values())

    subtotal = flight_cost + hotel_cost + add_on_total
    subtotal_after_promo = apply_promo(subtotal, promo_code)
    taxes_fees = get_taxes_and_fees(subtotal_after_promo)

    total = subtotal_after_promo + taxes_fees

    return {
        "passengers": passengers,
        "nights": nights,
        "flight_cost": round(flight_cost, 2),
        "hotel_cost": round(hotel_cost, 2),
        "add_ons": round(add_on_total, 2),
        "discount_applied": round(subtotal - subtotal_after_promo, 2),
        "taxes_fees": round(taxes_fees, 2),
        "total": round(total, 2),
        "promo_code": promo_code.upper() if promo_code else "NONE"
    }

# Example usage — perfect for a booking plugin demo
if __name__ == "__main__":
    booking = generate_travel_reservation(
        passengers=2,
        flight_base_fare=320.0,
        nights=7,
        nightly_rate=110.0,
        rooms=1,
        add_ons={"Airport Transfer": 45.0, "Travel Insurance": 28.0},
        promo_code="TRAVEL10"
    )

    print("═" * 55)
    print("     TRAVEL RESERVATION SUMMARY")
    print("═" * 55)
    print(f"Passengers        : {booking['passengers']}")
    print(f"Nights            : {booking['nights']}")
    print(f"Flight Cost       : €{booking['flight_cost']}")
    print(f"Hotel Cost        : €{booking['hotel_cost']}")
    print(f"Add-ons           : €{booking['add_ons']}")
    print(f"Promo ({booking['promo_code']})      : -€{booking['discount_applied']}")
    print(f"Taxes & Fees      : €{booking['taxes_fees']}")
    print("─" * 55)
    print(f"**TOTAL TO PAY    : €{booking['total']}**")
    print("═" * 55)
    print("Thank you for booking with [Your Travel Brand] ✈️")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)