I have a functioning distance calculator for my WordPress site that computes delivery fees based on the miles traveled. While the current setup works, I need to implement a minimum charge feature.
At the moment, my calculator displays whatever price is generated from the distance calculation. However, I want to guarantee that customers pay at least $40, even if the calculated cost is lower. For instance, if the distance only results in a $25 fee, it should show $40 instead.
Here is the code I’m currently using:
document.addEventListener('DOMContentLoaded', function() {
let mapInstance;
let routeService;
let routeDisplay;
function setupMap() {
routeService = new google.maps.DirectionsService();
routeDisplay = new google.maps.DirectionsRenderer();
mapInstance = new google.maps.Map(document.getElementById('route-map'), {
center: { lat: 40.7128, lng: -74.0060 },
zoom: 10
});
routeDisplay.setMap(mapInstance);
}
function computeRoute() {
const startPoint = document.getElementById('start-location').value;
const endPoint = document.getElementById('end-location').value;
if (startPoint && endPoint) {
const routeRequest = {
origin: startPoint,
destination: endPoint,
travelMode: 'DRIVING'
};
routeService.route(routeRequest, function(response, status) {
if (status == 'OK') {
routeDisplay.setDirections(response);
const totalDistance = response.routes[0].legs[0].distance.value / 1609.34; // convert to miles
const finalPrice = totalDistance * 3.5; // $3.50 per mile
document.getElementById('route-distance').textContent = totalDistance.toFixed(2) + ' miles';
document.getElementById('final-price').textContent = '$' + finalPrice.toFixed(2);
} else {
alert('Route calculation failed: ' + status);
}
});
} else {
alert('Both locations must be filled out.');
}
}
document.getElementById('calc-route').addEventListener('click', computeRoute);
setupMap();
});
I’ve been trying to add some conditional logic, but I can’t seem to get it to work right. Any advice on how to add this minimum price feature?