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
}
It looks like you've provided the same code block twice in your query, which defines the add_journey_type_after_waiting function. Since the two codes are identical, combining them doesn't require any changes—I'll simply present the code as a single, clean block.
However, you mentioned that the "second code has charges." In the provided code, there's already a reference to charges in the addToSummary function (e.g., it handles displaying and calculating charges for "Airport Trip"). The calculateCharges function is mentioned but not fully defined in your code (it's commented as "// Your existing charge calculation code remains the same"). If the second code was meant to include a specific implementation for calculateCharges, you'll need to provide that for a more accurate combination.
For now, I'll assume the combined code should include the existing structure, and I'll add a placeholder or sample implementation for calculateCharges based on common scenarios (e.g., calculating airport charges based on distance or other factors). If you have the actual charge calculation logic from the second code, please share it for further refinement.
Here's the combined code as a single, functional block:
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;"></div>
</div>
`;
$('#mptbm_waiting_time').closest('.inputList').after(journeyTypeHtml);
// Sample implementation for calculateCharges function
// This is a placeholder based on your description. Replace with your actual logic.
function calculateCharges() {
var startPlace = $('#mptbm_map_start_place').val();
var endPlace = $('#mptbm_map_end_place').val();
var charges = []; // Array to hold charges
if ($('#mptbm_journey_type').val() === 'airport_trip') {
// Example: Calculate charges based on distance or fixed fees
// In a real scenario, this could use API data, distance calculations, or predefined rules
var distance = 50; // Dummy value; replace with actual calculation (e.g., from Google Maps API)
if (distance > 20) {
charges.push('Airport Handling Fee: £10.00');
charges.push('Extra Mileage Charge: £5.00 per mile over 20 miles');
} else {
charges.push('Standard Airport Fee: £5.00');
}
// Display charges on the page (optional)
$('#charge_display').html('<h6>Calculated Charges:</h6>' + charges.join('<br>')).show();
return charges; // Return the array of charges
}
return charges; // Return empty array if not airport trip
}
// Function to add journey type and charges to summary
function addToSummary(journeyType, flightNumber, charges) {
var $transportSummary = $('.mptbm_transport_summary');
$('.journey-type-section').remove(); // Remove existing sections
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));
}
}
}
// 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(); // Calculate charges when airport trip is selected
} 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 can be added here if it's not already integrated
// For example: Any code related to updating the cart with charges
}
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
Below is the combined version of the two codes you provided. I'll explain the key steps I took to merge them:
HTML and Styling: I used the HTML from the second code for journeyTypeHtml because it includes additional styling (e.g., widths, floats, and margins). This makes the layout more specific.
calculateCharges Function: I took the fully implemented version from the second code, as it handles charge calculations based on locations. This is the core difference, as the first code had a placeholder.
addToSummary Function: This is present in the first code but not in the second, so I included it as-is. It handles adding journey details to the summary and updating the total price.
Event Handlers: I merged the event handlers from both codes. For example:
#mptbm_journey_type is similar, so I used the one from the second code and ensured it calls calculateCharges.calculateCharges integrated.Additional Elements from Second Code:
$('button#mptbm_get_vehicle').parent().css(...)) is added.add_action('woocommerce_cart_calculate_fees', 'add_airport_charges_to_cart')) from the second code is included at the end.Overall Structure: The combined code is wrapped in a single function to avoid duplication. I ensured there are no conflicts, such as overlapping functions (e.g., calculateCharges is overridden with the second code's version).
Here's the final combined 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() {
?>
<script>
jQuery(document).ready(function($) {
// Add journey type after waiting time dropdown with updated styling
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>
`;
$('#mptbm_waiting_time').closest('.inputList').after(journeyTypeHtml);
// Move and style search button
$('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');
}
// Function to add journey type and charges to summary (from first code)
function addToSummary(journeyType, flightNumber, charges) {
var $transportSummary = $('.mptbm_transport_summary');
$('.journey-type-section').remove(); // Remove existing sections
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 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 (from second code)
$('#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 (from first code)
$('#mptbm_map_start_place, #mptbm_map_end_place').on('change keyup', function() {
if($('#mptbm_journey_type').val() === 'airport_trip') {
calculateCharges();
}
});
// Handle car selection (from first code)
$(document).on('click', '.mptbm_transport_select', function() {
var journeyType = $('#mptbm_journey_type').val();
var flightNumber = $('#mptbm_flight_number').val();
var charges = calculateCharges(); // Use updated calculateCharges
addToSummary(journeyType, flightNumber, charges);
});
// Initial calculation
calculateCharges();
});
</script>
<?php
}
// Add charges to cart (from second code)
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]));
}
}
}
}
}
}
calculateCharges Function: Since you mentioned the second code has charges, I added a basic implementation for calculateCharges. This is just a placeholder—replace it with your actual logic (e.g., integrating with a distance API or database).calculateCharges function accordingly. For instance, it could fetch data from WordPress options or an external API.#mptbm_waiting_time).If this isn't what you intended, please provide more details about the differences in the two codes (e.g., the actual charge calculation logic from the second code), and I'll refine it further!
}
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]));
}
}
}
}
}
}
calculateCharges from the second code, which calculates and displays charges based on airport locations. It also stores charges for WooCommerce integration.addToSummary function from the first code is included, so it will still update the summary and total price when a car is selected.#mptbm_waiting_time). If you encounter issues, it might be due to page-specific HTML.If you need further adjustments or have additional details, let me know!