combine these both codes as second code have charges addaction('init',…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
combine these both codes as second code have charges
add_action('init', 'add_journey_type_after_waiting');
function add_journey_type_after_waiting() {
add_action('wp_footer', 'add_journey_type_script');
function add_journey_type_script() {
?>
jQuery(document).ready(function($) {
// Add journey type after waiting time dropdown
var journeyTypeHtml = <div class="inputList mp_input_select"> <label class="fdColumn"> <span>Journey Type</span> <select class="formControl" name="mptbm_journey_type" id="mptbm_journey_type"> <option value="">Select Journey Type</option> <option value="airport_trip">Airport Trip</option> <option value="business_trip">Business Trip</option> <option value="casual_trip">Casual Trip</option> </select> <i id="journey_type_icon" class="fas fa-plane mptbm_left_icon allCenter"></i> </label> <div class="flight-number" style="display:none;"> <label class="fdColumn"> <span>Flight Number</span> <input type="text" class="formControl" name="mptbm_flight_number" id="mptbm_flight_number" placeholder="Enter Flight Number"> <i class="fas fa-ticket-alt mptbm_left_icon allCenter"></i> </label> </div> <div id="charge_display" style="display:none;"></div> </div> ;
$('#mptbm_waiting_time').closest('.inputList').after(journeyTypeHtml);
function calculateCharges() {
// Your existing charge calculation code remains the same
}
// Function to add journey type and charges to summary
function addToSummary(journeyType, flightNumber, charges) {
// Find the location where we want to insert our new content
var $transportSummary = $('.mptbm_transport_summary');
// Remove any existing journey type sections
$('.journey-type-section').remove();
if(journeyType === 'airport_trip') {
// Create journey type section HTML
var journeyHtml = `
<div class="journey-type-section">
<div class="dividerL"></div>
<h6 class="_mB_xs">Journey Type</h6>
<p class="_textLight_1">Airport Trip</p>
`;
// Add flight number if exists
if(flightNumber) {
journeyHtml += `
<div class="dividerL"></div>
<h6 class="_mB_xs">Flight Number</h6>
<p class="_textLight_1">${flightNumber}</p>
`;
}
// Add charges if any
if(charges && charges.length > 0) {
journeyHtml += `
<div class="dividerL"></div>
<h6 class="_mB_xs">Airport Charges</h6>
`;
charges.forEach(function(charge) {
journeyHtml += `<p class="_textLight_1">${charge}</p>`;
});
}
journeyHtml += '</div>';
// Insert before transport summary
$transportSummary.before(journeyHtml);
// Update total price if charges exist
if(charges && charges.length > 0) {
var currentTotal = parseFloat($('.mptbm_product_total_price').text().replace('£', '')) || 0;
var chargeTotal = charges.reduce(function(total, charge) {
var amount = parseFloat(charge.split('£')[1]);
return total + (isNaN(amount) ? 0 : amount);
}, 0);
$('.mptbm_product_total_price').text('£' + (currentTotal + chargeTotal).toFixed(2));
}
}
}
// Handle journey type change
$('#mptbm_journey_type').change(function() {
var selectedValue = $(this).val();
var icon = $('#journey_type_icon');
// Your existing icon change code remains the same
switch(selectedValue) {
case 'airport_trip':
icon.removeClass().addClass('fas fa-plane mptbm_left_icon allCenter');
break;
case 'business_trip':
icon.removeClass().addClass('fas fa-briefcase mptbm_left_icon allCenter');
break;
case 'casual_trip':
icon.removeClass().addClass('fas fa-car mptbm_left_icon allCenter');
break;
default:
icon.removeClass().addClass('fas fa-plane mptbm_left_icon allCenter');
}
if(selectedValue === 'airport_trip') {
$('.flight-number').show();
$('#mptbm_flight_number').prop('required', true);
calculateCharges();
} else {
$('.flight-number').hide();
$('#mptbm_flight_number').prop('required', false).val('');
$('#charge_display').html('').hide();
}
});
// Monitor location changes
$('#mptbm_map_start_place, #mptbm_map_end_place').on('change keyup', function() {
if($('#mptbm_journey_type').val() === 'airport_trip') {
calculateCharges();
}
});
// Handle car selection
$(document).on('click', '.mptbm_transport_select', function() {
var journeyType = $('#mptbm_journey_type').val();
var flightNumber = $('#mptbm_flight_number').val();
var charges = calculateCharges();
addToSummary(journeyType, flightNumber, charges);
});
// Initial calculation
calculateCharges();
});
</script>
<?php
}
// Your existing cart calculation code remains the same
}
add_action('init', 'add_journey_type_after_waiting');
function add_journey_type_after_waiting() {
add_action('wp_footer', 'add_journey_type_script');
function add_journey_type_script() {
?>
jQuery(document).ready(function($) {
// Add journey type after waiting time dropdown
var journeyTypeHtml = <div class="inputList mp_input_select"> <label class="fdColumn"> <span>Journey Type</span> <select class="formControl" name="mptbm_journey_type" id="mptbm_journey_type"> <option value="">Select Journey Type</option> <option value="airport_trip">Airport Trip</option> <option value="business_trip">Business Trip</option> <option value="casual_trip">Casual Trip</option> </select> <i id="journey_type_icon" class="fas fa-plane mptbm_left_icon allCenter"></i> </label> <div class="flight-number" style="display:none;"> <label class="fdColumn"> <span>Flight Number</span> <input type="text" class="formControl" name="mptbm_flight_number" id="mptbm_flight_number" placeholder="Enter Flight Number"> <i class="fas fa-ticket-alt mptbm_left_icon allCenter"></i> </label> </div> <div id="charge_display" style="display:none;"></div> </div> ;
$('#mptbm_waiting_time').closest('.inputList').after(journeyTypeHtml);
function calculateCharges() {
// Your existing charge calculation code remains the same
}
// Function to add journey type and charges to summary
function addToSummary(journeyType, flightNumber, charges) {
// Find the location where we want to insert our new content
var $transportSummary = $('.mptbm_transport_summary');
// Remove any existing journey type sections
$('.journey-type-section').remove();
if(journeyType === 'airport_trip') {
// Create journey type section HTML
var journeyHtml = `
<div class="journey-type-section">
<div class="dividerL"></div>
<h6 class="_mB_xs">Journey Type</h6>
<p class="_textLight_1">Airport Trip</p>
`;
// Add flight number if exists
if(flightNumber) {
journeyHtml += `
<div class="dividerL"></div>
<h6 class="_mB_xs">Flight Number</h6>
<p class="_textLight_1">${flightNumber}</p>
`;
}
// Add charges if any
if(charges && charges.length > 0) {
journeyHtml += `
<div class="dividerL"></div>
<h6 class="_mB_xs">Airport Charges</h6>
`;
charges.forEach(function(charge) {
journeyHtml += `<p class="_textLight_1">${charge}</p>`;
});
}
journeyHtml += '</div>';
// Insert before transport summary
$transportSummary.before(journeyHtml);
// Update total price if charges exist
if(charges && charges.length > 0) {
var currentTotal = parseFloat($('.mptbm_product_total_price').text().replace('£', '')) || 0;
var chargeTotal = charges.reduce(function(total, charge) {
var amount = parseFloat(charge.split('£')[1]);
return total + (isNaN(amount) ? 0 : amount);
}, 0);
$('.mptbm_product_total_price').text('£' + (currentTotal + chargeTotal).toFixed(2));
}
}
}
// Handle journey type change
$('#mptbm_journey_type').change(function() {
var selectedValue = $(this).val();
var icon = $('#journey_type_icon');
// Your existing icon change code remains the same
switch(selectedValue) {
case 'airport_trip':
icon.removeClass().addClass('fas fa-plane mptbm_left_icon allCenter');
break;
case 'business_trip':
icon.removeClass().addClass('fas fa-briefcase mptbm_left_icon allCenter');
break;
case 'casual_trip':
icon.removeClass().addClass('fas fa-car mptbm_left_icon allCenter');
break;
default:
icon.removeClass().addClass('fas fa-plane mptbm_left_icon allCenter');
}
if(selectedValue === 'airport_trip') {
$('.flight-number').show();
$('#mptbm_flight_number').prop('required', true);
calculateCharges();
} else {
$('.flight-number').hide();
$('#mptbm_flight_number').prop('required', false).val('');
$('#charge_display').html('').hide();
}
});
// Monitor location changes
$('#mptbm_map_start_place, #mptbm_map_end_place').on('change keyup', function() {
if($('#mptbm_journey_type').val() === 'airport_trip') {
calculateCharges();
}
});
// Handle car selection
$(document).on('click', '.mptbm_transport_select', function() {
var journeyType = $('#mptbm_journey_type').val();
var flightNumber = $('#mptbm_flight_number').val();
var charges = calculateCharges();
addToSummary(journeyType, flightNumber, charges);
});
// Initial calculation
calculateCharges();
});
</script>
<?php
}
// Your existing cart calculation code remains the same
}
I notice you have two identical code blocks, but you mentioned the second one has charges functionality. Since the code appears incomplete (the calculateCharges() function is empty), I'll combine them and add a complete charges calculation implementation for airport trips:
add_action('init', 'add_journey_type_after_waiting');
function add_journey_type_after_waiting() {
add_action('wp_footer', 'add_journey_type_script');
function add_journey_type_script() {
?>
<script>
jQuery(document).ready(function($) {
// Add journey type after waiting time dropdown
var journeyTypeHtml = `
<div class="inputList mp_input_select">
<label class="fdColumn">
<span>Journey Type</span>
<select class="formControl" name="mptbm_journey_type" id="mptbm_journey_type">
<option value="">Select Journey Type</option>
<option value="airport_trip">Airport Trip</option>
<option value="business_trip">Business Trip</option>
<option value="casual_trip">Casual Trip</option>
</select>
<i id="journey_type_icon" class="fas fa-plane mptbm_left_icon allCenter"></i>
</label>
<div class="flight-number" style="display:none;">
<label class="fdColumn">
<span>Flight Number</span>
<input type="text" class="formControl" name="mptbm_flight_number" id="mptbm_flight_number" placeholder="Enter Flight Number">
<i class="fas fa-ticket-alt mptbm_left_icon allCenter"></i>
</label>
</div>
<div id="charge_display" style="display:none; margin-top: 10px; padding: 10px; background: #f0f0f0; border-radius: 5px;"></div>
</div>
`;
$('#mptbm_waiting_time').closest('.inputList').after(journeyTypeHtml);
// Define airport charges
var airportCharges = {
'heathrow': {
name: 'Heathrow Airport',
dropOff: 5.00,
pickUp: 5.00,
parking: 8.00
},
'gatwick': {
name: 'Gatwick Airport',
dropOff: 5.00,
pickUp: 5.00,
parking: 6.00
},
'stansted': {
name: 'Stansted Airport',
dropOff: 7.00,
pickUp: 7.00,
parking: 9.00
},
'luton': {
name: 'Luton Airport',
dropOff: 5.00,
pickUp: 5.00,
parking: 7.00
},
'city': {
name: 'London City Airport',
dropOff: 0.00,
pickUp: 0.00,
parking: 5.00
}
};
function detectAirport(location) {
if (!location) return null;
var locationLower = location.toLowerCase();
for (var key in airportCharges) {
if (locationLower.includes(key) || locationLower.includes(airportCharges[key].name.toLowerCase())) {
return key;
}
}
return null;
}
function calculateCharges() {
var startLocation = $('#mptbm_map_start_place').val();
var endLocation = $('#mptbm_map_end_place').val();
var charges = [];
var totalCharge = 0;
// Check if start location is an airport (pickup charge)
var startAirport = detectAirport(startLocation);
if (startAirport && airportCharges[startAirport]) {
charges.push({
type: 'Pick-up Charge',
airport: airportCharges[startAirport].name,
amount: airportCharges[startAirport].pickUp
});
totalCharge += airportCharges[startAirport].pickUp;
}
// Check if end location is an airport (drop-off charge)
var endAirport = detectAirport(endLocation);
if (endAirport && airportCharges[endAirport]) {
charges.push({
type: 'Drop-off Charge',
airport: airportCharges[endAirport].name,
amount: airportCharges[endAirport].dropOff
});
totalCharge += airportCharges[endAirport].dropOff;
}
// Check waiting time for parking charges
var waitingTime = $('#mptbm_waiting_time').val();
if (waitingTime && parseInt(waitingTime) > 0) {
var airport = startAirport || endAirport;
if (airport && airportCharges[airport]) {
charges.push({
type: 'Parking Charge (' + waitingTime + ' mins)',
airport: airportCharges[airport].name,
amount: airportCharges[airport].parking
});
totalCharge += airportCharges[airport].parking;
}
}
// Display charges
if (charges.length > 0) {
var chargeHtml = '<h6 style="margin-bottom: 10px;">Airport Charges:</h6>';
charges.forEach(function(charge) {
chargeHtml += '<div style="display: flex; justify-content: space-between; margin-bottom: 5px;">';
chargeHtml += '<span>' + charge.type + ' - ' + charge.airport + '</span>';
chargeHtml += '<span style="font-weight: bold;">£' + charge.amount.toFixed(2) + '</span>';
chargeHtml += '</div>';
});
chargeHtml += '<div style="border-top: 1px solid #ccc; margin-top: 10px; padding-top: 10px; display: flex; justify-content: space-between;">';
chargeHtml += '<span style="font-weight: bold;">Total Airport Charges:</span>';
chargeHtml += '<span style="font-weight: bold; color: #e74c3c;">£' + totalCharge.toFixed(2) + '</span>';
chargeHtml += '</div>';
$('#charge_display').html(chargeHtml).show();
} else {
$('#charge_display').html('').hide();
}
return charges.map(function(charge) {
return charge.type + ' - ' + charge.airport + ': £' + charge.amount.toFixed(2);
});
}
// Function to add journey type and charges to summary
function addToSummary(journeyType, flightNumber, charges) {
var $transportSummary = $('.mptbm_transport_summary');
// Remove any existing journey type sections
$('.journey-type-section').remove();
if(journeyType === 'airport_trip') {
var journeyHtml = `
<div class="journey-type-section">
<div class="dividerL"></div>
<h6 class="_mB_xs">Journey Type</h6>
<p class="_textLight_1">Airport Trip</p>
`;
if(flightNumber) {
journeyHtml += `
<div class="dividerL"></div>
<h6 class="_mB_xs">Flight Number</h6>
<p class="_textLight_1">${flightNumber}</p>
`;
}
if(charges && charges.length > 0) {
journeyHtml += `
<div class="dividerL"></div>
<h6 class="_mB_xs">Airport Charges</h6>
`;
charges.forEach(function(charge) {
journeyHtml += `<p class="_textLight_1">${charge}</p>`;
});
}
journeyHtml += '</div>';
$transportSummary.before(journeyHtml);
// Update total price
if(charges && charges.length > 0) {
var currentTotal = parseFloat($('.mptbm_product_total_price').text().replace('£', '')) || 0;
var chargeTotal = charges.reduce(function(total, charge) {
var amount = parseFloat(charge.split('£')[1]);
return total + (isNaN(amount) ? 0 : amount);
}, 0);
$('.mptbm_product_total_price').text('£' + (currentTotal + chargeTotal).toFixed(2));
}
} else if(journeyType) {
// For other journey types
var journeyTypeText = journeyType === 'business_trip' ? 'Business Trip' : 'Casual Trip';
var journeyHtml = `
<div class="journey-type-section">
<div class="dividerL"></div>
<h6 class="_mB_xs">Journey Type</h6>
<p class="_textLight_1">${journeyTypeText}</p>
</div>
`;
$transportSummary.before(journeyHtml);
}
}
// Handle journey type change
$('#mptbm_journey_type').change(function() {
var selectedValue = $(this).val();
var icon = $('#journey_type_icon');
switch(selectedValue) {
case 'airport_trip':
icon.removeClass().addClass('fas fa-plane mptbm_left_icon allCenter');
break;
case 'business_trip':
icon.removeClass().addClass('fas fa-briefcase mptbm_left_icon allCenter');
break;
case 'casual_trip':
icon.removeClass().addClass('fas fa-car mptbm_left_icon allCenter');
break;
default:
icon.removeClass().addClass('fas fa-plane mptbm_left_icon allCenter');
}
if(selectedValue === 'airport_trip') {
$('.flight-number').show();
$('#mptbm_flight_number').prop('required', true);
calculateCharges();
} else {
$('.flight-number').hide();
$('#mptbm_flight_number').prop('required', false).val('');
$('#charge_display').html('').hide();
}
});
// Monitor location changes
$('#mptbm_map_start_place, #mptbm_map_end_place').on('change keyup', function() {
if($('#mptbm_journey_type').val() === 'airport_trip') {
calculateCharges();
}
});
// Monitor waiting time changes
$('#mptbm_waiting_time').on('change', function() {
if($('#mptbm_journey_type').val() === 'airport_trip') {
calculateCharges();
}
});
// Handle car selection
$(document).on('click', '.mptbm_transport_select', function() {
var journeyType = $('#mptbm_journey_type').val();
var flightNumber = $('#mptbm_flight_number').val();
var charges = [];
if(journeyType === 'airport_trip') {
charges = calculateCharges();
}
addToSummary(journeyType, flightNumber, charges);
});
// Store charges in hidden field for backend processing
$('form').on('submit', function() {
if($('#mptbm_journey_type').val() === 'airport_trip') {
var charges = calculateCharges();
var totalCharge = charges.reduce(function(total, charge) {
var amount = parseFloat(charge.split('£')[1]);
return total + (isNaN(amount) ? 0 : amount);
}, 0);
// Add hidden field with charge data
if($('#airport_charges_total').length === 0) {
$(this).append('<input type="hidden" id="airport_charges_total" name="airport_charges_total" value="' + totalCharge + '">');
$(this).append('<input type="hidden" name="airport_charges_details" value="' + JSON.stringify(charges) + '">');
} else {
$('#airport_charges_total').val(totalCharge);
$('[name="airport_charges_details"]').val(JSON.stringify(charges));
}
}
});
});
</script>
<?php
}
// Add filter to modify cart item price
add_filter('woocommerce_add_cart_item_data', 'add_airport_charges_to_cart_item', 10, 3);
function add_airport_charges_to_cart_item($cart_item_data, $product_id, $variation_id) {
if(isset($_POST['mptbm_journey_type']) && $_POST['mptbm_journey_type'] === 'airport_trip') {
$cart_item_data['journey_type'] = sanitize_text_field($_POST['mptbm_journey_type']);
if(isset($_POST['mptbm_flight_number'])) {
$cart_item_data['flight_number'] = sanitize_text_field($_POST['mptbm_flight_number']);
}
if(isset($_POST['airport_charges_total'])) {
$cart_item_data['airport_charges'] = floatval($_POST['airport_charges_total']);
}
if(isset($_POST['airport_charges_details'])) {
$cart_item_data['airport_charges_details'] = $_POST['airport_charges_details'];
}
}
return $cart_item_data;
}
// Modify cart item price
add_action('woocommerce_before_calculate_totals', 'add_airport_charges_to_price', 20, 1);
function add_airport_charges_to_price($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
foreach ($cart->get_cart() as $cart_item) {
if(isset($cart_item['airport_charges']) && $cart_item['airport_charges'] > 0) {
$original_price = $cart_item['data']->get_price();
$new_price = $original_price + $cart_item['airport_charges'];
$cart_item['data']->set_price($new_price);
}
}
}
}
combine these both codes as 2nd code have charges
add_action('init', 'add_journey_type_after_waiting');
function add_journey_type_after_waiting() {
add_action('wp_footer', 'add_journey_type_script');
function add_journey_type_script() {
?>
jQuery(document).ready(function($) {
// Add journey type after waiting time dropdown
var journeyTypeHtml = <div class="inputList mp_input_select"> <label class="fdColumn"> <span>Journey Type</span> <select class="formControl" name="mptbm_journey_type" id="mptbm_journey_type"> <option value="">Select Journey Type</option> <option value="airport_trip">Airport Trip</option> <option value="business_trip">Business Trip</option> <option value="casual_trip">Casual Trip</option> </select> <i id="journey_type_icon" class="fas fa-plane mptbm_left_icon allCenter"></i> </label> <div class="flight-number" style="display:none;"> <label class="fdColumn"> <span>Flight Number</span> <input type="text" class="formControl" name="mptbm_flight_number" id="mptbm_flight_number" placeholder="Enter Flight Number"> <i class="fas fa-ticket-alt mptbm_left_icon allCenter"></i> </label> </div> <div id="charge_display" style="display:none;"></div> </div> ;
$('#mptbm_waiting_time').closest('.inputList').after(journeyTypeHtml);
function calculateCharges() {
// Your existing charge calculation code remains the same
}
// Function to add journey type and charges to summary
function addToSummary(journeyType, flightNumber, charges) {
// Find the location where we want to insert our new content
var $transportSummary = $('.mptbm_transport_summary');
// Remove any existing journey type sections
$('.journey-type-section').remove();
if(journeyType === 'airport_trip') {
// Create journey type section HTML
var journeyHtml = `
<div class="journey-type-section">
<div class="dividerL"></div>
<h6 class="_mB_xs">Journey Type</h6>
<p class="_textLight_1">Airport Trip</p>
`;
// Add flight number if exists
if(flightNumber) {
journeyHtml += `
<div class="dividerL"></div>
<h6 class="_mB_xs">Flight Number</h6>
<p class="_textLight_1">${flightNumber}</p>
`;
}
// Add charges if any
if(charges && charges.length > 0) {
journeyHtml += `
<div class="dividerL"></div>
<h6 class="_mB_xs">Airport Charges</h6>
`;
charges.forEach(function(charge) {
journeyHtml += `<p class="_textLight_1">${charge}</p>`;
});
}
journeyHtml += '</div>';
// Insert before transport summary
$transportSummary.before(journeyHtml);
// Update total price if charges exist
if(charges && charges.length > 0) {
var currentTotal = parseFloat($('.mptbm_product_total_price').text().replace('£', '')) || 0;
var chargeTotal = charges.reduce(function(total, charge) {
var amount = parseFloat(charge.split('£')[1]);
return total + (isNaN(amount) ? 0 : amount);
}, 0);
$('.mptbm_product_total_price').text('£' + (currentTotal + chargeTotal).toFixed(2));
}
}
}
// Handle journey type change
$('#mptbm_journey_type').change(function() {
var selectedValue = $(this).val();
var icon = $('#journey_type_icon');
// Your existing icon change code remains the same
switch(selectedValue) {
case 'airport_trip':
icon.removeClass().addClass('fas fa-plane mptbm_left_icon allCenter');
break;
case 'business_trip':
icon.removeClass().addClass('fas fa-briefcase mptbm_left_icon allCenter');
break;
case 'casual_trip':
icon.removeClass().addClass('fas fa-car mptbm_left_icon allCenter');
break;
default:
icon.removeClass().addClass('fas fa-plane mptbm_left_icon allCenter');
}
if(selectedValue === 'airport_trip') {
$('.flight-number').show();
$('#mptbm_flight_number').prop('required', true);
calculateCharges();
} else {
$('.flight-number').hide();
$('#mptbm_flight_number').prop('required', false).val('');
$('#charge_display').html('').hide();
}
});
// Monitor location changes
$('#mptbm_map_start_place, #mptbm_map_end_place').on('change keyup', function() {
if($('#mptbm_journey_type').val() === 'airport_trip') {
calculateCharges();
}
});
// Handle car selection
$(document).on('click', '.mptbm_transport_select', function() {
var journeyType = $('#mptbm_journey_type').val();
var flightNumber = $('#mptbm_flight_number').val();
var charges = calculateCharges();
addToSummary(journeyType, flightNumber, charges);
});
// Initial calculation
calculateCharges();
});
</script>
<?php
}
// Your existing cart calculation code remains the same
Here's the combined code that merges both versions, incorporating the charges functionality from the second code and the summary display from the first:
add_action('init', 'add_journey_type_after_waiting');
function add_journey_type_after_waiting() {
add_action('wp_footer', 'add_journey_type_script');
function add_journey_type_script() {
?>
<script>
jQuery(document).ready(function($) {
// Add journey type after waiting time dropdown
var journeyTypeHtml = `
<div class="inputList mp_input_select" style="width: calc(100% - 320px); float: left; margin-right: 20px;">
<label class="fdColumn">
<span>Journey Type</span>
<select class="formControl" name="mptbm_journey_type" id="mptbm_journey_type">
<option value="">Select Journey Type</option>
<option value="airport_trip">Airport Trip</option>
<option value="business_trip">Business Trip</option>
<option value="casual_trip">Casual Trip</option>
</select>
<i id="journey_type_icon" class="fas fa-plane mptbm_left_icon allCenter"></i>
</label>
<div class="flight-number" style="display:none; margin-top: 10px;">
<label class="fdColumn">
<span>Flight Number</span>
<input type="text" class="formControl" name="mptbm_flight_number" id="mptbm_flight_number" placeholder="Enter Flight Number">
<i class="fas fa-ticket-alt mptbm_left_icon allCenter"></i>
</label>
</div>
<div id="charge_display" style="display:none; margin-top: 10px;"></div>
</div>
`;
// Insert after waiting time
$('#mptbm_waiting_time').closest('.inputList').after(journeyTypeHtml);
// Move search button next to Journey Type
$('button#mptbm_get_vehicle').parent().css({
'width': '200px',
'float': 'left',
'margin-top': '20px'
});
function calculateCharges() {
if($('#mptbm_journey_type').val() !== 'airport_trip') {
$('#charge_display').html('').hide();
return [];
}
var pickup = $('#mptbm_map_start_place').val().toLowerCase();
var dropoff = $('#mptbm_map_end_place').val().toLowerCase();
var charges = [];
var total = 0;
// Clear previous charges
$('#charge_display').html('');
// Pickup Location Charges
if(pickup.includes('airport')) {
if(pickup.includes('heathrow')) {
charges.push('Heathrow Airport Car Park Charge: £7.50');
total += 7.50;
} else if(pickup.includes('gatwick') || pickup.includes('luton') || pickup.includes('stansted')) {
var airportName = pickup.includes('gatwick') ? 'Gatwick' :
pickup.includes('luton') ? 'Luton' : 'Stansted';
charges.push(airportName + ' Airport Car Park Charge: £7.00');
total += 7.00;
}
}
// Drop-off Location Charges
if(dropoff.includes('airport')) {
if(dropoff.includes('heathrow')) {
charges.push('Heathrow Airport Drop-off Charge: £6.00');
total += 6.00;
} else if(dropoff.includes('gatwick')) {
charges.push('Gatwick Airport Drop-off Charge: £7.00');
total += 7.00;
} else if(dropoff.includes('stansted')) {
charges.push('Stansted Airport Drop-off Charge: £7.00');
total += 7.00;
} else if(dropoff.includes('luton')) {
charges.push('Luton Airport Drop-off Charge: £5.00');
total += 5.00;
}
}
// Display charges if any
if(charges.length > 0) {
$('#charge_display').html(charges.join('<br>')).show();
if(charges.length > 1) {
$('#charge_display').append('<br><strong>Total Extra Charges: £' + total.toFixed(2) + '</strong>');
}
} else {
$('#charge_display').hide();
}
// Store charges for checkout
$('input[name="airport_charges"]').remove(); // Remove existing hidden input
$('<input>').attr({
type: 'hidden',
name: 'airport_charges',
value: JSON.stringify({
pickup: pickup,
dropoff: dropoff,
charges: charges,
total: total
})
}).appendTo('form');
return charges;
}
// Function to add journey type and charges to summary
function addToSummary(journeyType, flightNumber, charges) {
// Find the location where we want to insert our new content
var $transportSummary = $('.mptbm_transport_summary');
// Remove any existing journey type sections
$('.journey-type-section').remove();
if(journeyType === 'airport_trip') {
// Create journey type section HTML
var journeyHtml = `
<div class="journey-type-section">
<div class="dividerL"></div>
<h6 class="_mB_xs">Journey Type</h6>
<p class="_textLight_1">Airport Trip</p>
`;
// Add flight number if exists
if(flightNumber) {
journeyHtml += `
<div class="dividerL"></div>
<h6 class="_mB_xs">Flight Number</h6>
<p class="_textLight_1">${flightNumber}</p>
`;
}
// Add charges if any
if(charges && charges.length > 0) {
journeyHtml += `
<div class="dividerL"></div>
<h6 class="_mB_xs">Airport Charges</h6>
`;
charges.forEach(function(charge) {
journeyHtml += `<p class="_textLight_1">${charge}</p>`;
});
}
journeyHtml += '</div>';
// Insert before transport summary
$transportSummary.before(journeyHtml);
// Update total price if charges exist
if(charges && charges.length > 0) {
var currentTotal = parseFloat($('.mptbm_product_total_price').text().replace('£', '')) || 0;
var chargeTotal = charges.reduce(function(total, charge) {
var amount = parseFloat(charge.split('£')[1]);
return total + (isNaN(amount) ? 0 : amount);
}, 0);
$('.mptbm_product_total_price').text('£' + (currentTotal + chargeTotal).toFixed(2));
}
} else if(journeyType === 'business_trip') {
var journeyHtml = `
<div class="journey-type-section">
<div class="dividerL"></div>
<h6 class="_mB_xs">Journey Type</h6>
<p class="_textLight_1">Business Trip</p>
</div>
`;
$transportSummary.before(journeyHtml);
} else if(journeyType === 'casual_trip') {
var journeyHtml = `
<div class="journey-type-section">
<div class="dividerL"></div>
<h6 class="_mB_xs">Journey Type</h6>
<p class="_textLight_1">Casual Trip</p>
</div>
`;
$transportSummary.before(journeyHtml);
}
}
// Handle journey type change
$('#mptbm_journey_type').change(function() {
var selectedValue = $(this).val();
var icon = $('#journey_type_icon');
// Change icon based on selection
switch(selectedValue) {
case 'airport_trip':
icon.removeClass().addClass('fas fa-plane mptbm_left_icon allCenter');
break;
case 'business_trip':
icon.removeClass().addClass('fas fa-briefcase mptbm_left_icon allCenter');
break;
case 'casual_trip':
icon.removeClass().addClass('fas fa-car mptbm_left_icon allCenter');
break;
default:
icon.removeClass().addClass('fas fa-plane mptbm_left_icon allCenter');
}
if(selectedValue === 'airport_trip') {
$('.flight-number').show();
$('#mptbm_flight_number').prop('required', true);
calculateCharges();
} else {
$('.flight-number').hide();
$('#mptbm_flight_number').prop('required', false).val('');
$('#charge_display').html('').hide();
}
});
// Monitor location changes
$('#mptbm_map_start_place, #mptbm_map_end_place').on('change keyup', function() {
if($('#mptbm_journey_type').val() === 'airport_trip') {
calculateCharges();
}
});
// Handle car selection
$(document).on('click', '.mptbm_transport_select', function() {
var journeyType = $('#mptbm_journey_type').val();
var flightNumber = $('#mptbm_flight_number').val();
var charges = calculateCharges();
addToSummary(journeyType, flightNumber, charges);
});
// Initial calculation
calculateCharges();
});
</script>
<?php
}
// Add charges to cart
add_action('woocommerce_cart_calculate_fees', 'add_airport_charges_to_cart');
function add_airport_charges_to_cart($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
if (isset($_POST['airport_charges'])) {
$charges = json_decode(stripslashes($_POST['airport_charges']), true);
if($charges && isset($charges['charges'])) {
foreach($charges['charges'] as $charge) {
$parts = explode(': £', $charge);
if(count($parts) == 2) {
$cart->add_fee($parts[0], floatval($parts[1]));
}
}
}
}
}
}
This combined code includes:
The charges are configurable in the `air
}
2nd code
add_action('init', 'add_journey_type_after_waiting');
function add_journey_type_after_waiting() {
add_action('wp_footer', 'add_journey_type_script');
function add_journey_type_script() {
?>
jQuery(document).ready(function($) {
// Add journey type after waiting time dropdown
var journeyTypeHtml = <div class="inputList mp_input_select" style="width: calc(100% - 320px); float: left; margin-right: 20px;"> <label class="fdColumn"> <span>Journey Type</span> <select class="formControl" name="mptbm_journey_type" id="mptbm_journey_type"> <option value="">Select Journey Type</option> <option value="airport_trip">Airport Trip</option> <option value="business_trip">Business Trip</option> <option value="casual_trip">Casual Trip</option> </select> <i id="journey_type_icon" class="fas fa-plane mptbm_left_icon allCenter"></i> </label> <div class="flight-number" style="display:none; margin-top: 10px;"> <label class="fdColumn"> <span>Flight Number</span> <input type="text" class="formControl" name="mptbm_flight_number" id="mptbm_flight_number" placeholder="Enter Flight Number"> <i class="fas fa-ticket-alt mptbm_left_icon allCenter"></i> </label> </div> <div id="charge_display" style="display:none; margin-top: 10px;"></div> </div> ;
// Insert after waiting time
$('#mptbm_waiting_time').closest('.inputList').after(journeyTypeHtml);
// Move search button next to Journey Type
$('button#mptbm_get_vehicle').parent().css({
'width': '200px',
'float': 'left',
'margin-top': '20px'
});
function calculateCharges() {
if($('#mptbm_journey_type').val() !== 'airport_trip') {
$('#charge_display').html('').hide();
return;
}
var pickup = $('#mptbm_map_start_place').val().toLowerCase();
var dropoff = $('#mptbm_map_end_place').val().toLowerCase();
var charges = [];
var total = 0;
// Clear previous charges
$('#charge_display').html('');
// Pickup Location Charges
if(pickup.includes('airport')) {
if(pickup.includes('heathrow')) {
charges.push('Heathrow Airport Car Park Charge: £7.50');
total += 7.50;
} else if(pickup.includes('gatwick') || pickup.includes('luton') || pickup.includes('stansted')) {
var airportName = pickup.includes('gatwick') ? 'Gatwick' :
pickup.includes('luton') ? 'Luton' : 'Stansted';
charges.push(airportName + ' Airport Car Park Charge: £7.00');
total += 7.00;
}
}
// Drop-off Location Charges
if(dropoff.includes('airport')) {
if(dropoff.includes('heathrow')) {
charges.push('Heathrow Airport Drop-off Charge: £6.00');
total += 6.00;
} else if(dropoff.includes('gatwick')) {
charges.push('Gatwick Airport Drop-off Charge: £7.00');
total += 7.00;
} else if(dropoff.includes('stansted')) {
charges.push('Stansted Airport Drop-off Charge: £7.00');
total += 7.00;
} else if(dropoff.includes('luton')) {
charges.push('Luton Airport Drop-off Charge: £5.00');
total += 5.00;
}
}
// Display charges if any
if(charges.length > 0) {
$('#charge_display').html(charges.join('<br>')).show();
if(charges.length > 1) {
$('#charge_display').append('<br><strong>Total Extra Charges: £' + total.toFixed(2) + '</strong>');
}
} else {
$('#charge_display').hide();
}
// Store charges for checkout
$('<input>').attr({
type: 'hidden',
name: 'airport_charges',
value: JSON.stringify({
pickup: pickup,
dropoff: dropoff,
charges: charges,
total: total
})
}).appendTo('form');
}
// Handle journey type change
$('#mptbm_journey_type').change(function() {
var selectedValue = $(this).val();
var icon = $('#journey_type_icon');
// Change icon based on selection
switch(selectedValue) {
case 'airport_trip':
icon.removeClass().addClass('fas fa-plane mptbm_left_icon allCenter');
break;
case 'business_trip':
icon.removeClass().addClass('fas fa-briefcase mptbm_left_icon allCenter');
break;
case 'casual_trip':
icon.removeClass().addClass('fas fa-car mptbm_left_icon allCenter');
break;
default:
icon.removeClass().addClass('fas fa-plane mptbm_left_icon allCenter');
}
if(selectedValue === 'airport_trip') {
$('.flight-number').show();
$('#mptbm_flight_number').prop('required', true);
calculateCharges();
} else {
$('.flight-number').hide();
$('#mptbm_flight_number').prop('required', false).val('');
$('#charge_display').html('').hide();
}
});
// Monitor location changes
$('#mptbm_map_start_place, #mptbm_map_end_place').on('change keyup', function() {
if($('#mptbm_journey_type').val() === 'airport_trip') {
calculateCharges();
}
});
// Initial calculation
calculateCharges();
});
</script>
<?php
}
// Add charges to cart
add_action('woocommerce_cart_calculate_fees', 'add_airport_charges_to_cart');
function add_airport_charges_to_cart($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
if (isset($_POST['airport_charges'])) {
$charges = json_decode(stripslashes($_POST['airport_charges']), true);
if($charges && isset($charges['charges'])) {
foreach($charges['charges'] as $charge) {
$parts = explode(': £', $charge);
if(count($parts) == 2) {
$cart->add_fee($parts[0], floatval($parts[1]));
}
}
}
}
}
}
This combined code includes:
The charges are automatically calculated when locations are entered or changed, and the total price is updated accordingly in both the display and the cart.