Question
I want deliveryCost to be per order. for example if i ordered 1 shirt and 1 tie, that would be one order. eg i ordered
I want deliveryCost to be per order. for example if i ordered 1 shirt and 1 tie, that would be one order.
eg i ordered 1 tie and 1 shirt on 7 days ( 2 per day). i want the results: the total delivery cost to be 2 not 4 as i get in the code. please help.
its to do with i think : deliveryCost = deliveryCostPer * totalQty;
and also could u make sure that if no shirt and/or tie are selected that the "Nan" does not come up in the results. thanks.
js file:
(function () { "use strict"; var state = document.getElementById('s-state');
document.addEventListener('DOMContentLoaded', function () { document.getElementById('cart-hplus').addEventListener('submit', estimateTotal); var btnEstimate = document.getElementById('btn-estimate'); btnEstimate.disabled = true; state.addEventListener('change', function () { if (state.value === '') { btnEstimate.disabled = true; } else { btnEstimate.disabled = false; } }); });
function estimateTotal(event) { event.preventDefault(); if (state.value === '') { alert('Please choose your customer state.'); state.focus(); } var itemshirt = parseInt(document.getElementById('cottonshirt').value, 10) || 0, itemtie = parseInt(document.getElementById('cottontie').value, 10) || 0, customerState = state.value, deliveryMethod = document.querySelector('[name=r_method]:checked').value || ""; var totalQty = itemshirt + itemtie, deliveryCostPer, deliveryCost, discountFactor = 1, estimate, total, discount, totalItemPrice = (20 * itemshirt) + (12 * itemtie);
var isDiscounted = false;
if (customerState === "RC") { discountFactor = 1.10; //Need to minus 10% from total amount (estimate). please.help isDiscounted = true; } else if (customerState === "TC") { discountFactor = 1.20; //Need to minus 20% from total amount(estimate). please.help isDiscounted = true; } switch (deliveryMethod) { case "7-days": deliveryCostPer = 2; break;
case "3-days": deliveryCostPer = 3; break; case "next-day": deliveryCostPer = 5; break; }
if (checkforNameInputs()) { deliveryCost = deliveryCostPer * totalQty; //you are saying delivery cost per order, so don't multiply with total quantity if you mean to per order. discount = ((discountFactor - 1) * totalItemPrice).toFixed(2); //Here I calculated discount Separately estimate = '' + (totalItemPrice + deliveryCost); // so estimate = (totalItemPrice) + deliveryCost total = ((totalItemPrice + deliveryCost) - discount); //Subtracted discount from total item price + delivery cost
document.getElementById('txt-estimate').value = estimate; var fname = document.getElementById('first').value; var last = document.getElementById('last').value; var results = document.getElementById('results'); var shirtMsg = "shirt"; var tieMsg = "tie"; if (itemshirt > 1) { shirtMsg = "Shirts"; }
if (itemtie > 1) { tieMsg = "ties"; }
results.innerHTML = 'hello ' + fname + " " + last; if(itemshirt > 0) results.innerHTML += ' ' + 'You have selected: ' + itemshirt + ' ' + shirtMsg +' @ 20 each. ' + ' Subtotal = ' + (20 * itemshirt); if(itemtie > 0) results.innerHTML += ' ' + itemtie + ' ' + tieMsg + ' @ 12 each' + ' Subtotal =' + (12 * itemtie); results.innerHTML += ' ' + 'Total cost of delivery: ' + deliveryCost + ' '; results.innerHTML += 'Total cost ' + estimate + " "; // Moved estimate below Delivery cost results.innerHTML += 'Discount: ' + ((discountFactor - 1) * 100).toFixed(2) + '% (' + customerState + ')' + ' '; if(isDiscounted===true) { results.innerHTML+= 'You have qualified for a discount: ' + discount + ' results.innerHTML += 'Cost discount included: ' + total + ' ' + "If you're happy with that, please go ahead and place the order. Have an awesome day"; } } })(); function checkforNameInputs() { var firstName = document.getElementById('first').value; var lastName = document.getElementById('last').value; if (firstName == "") { window.alert("Please enter firstname"); return false; } else if (lastName == "") { window.alert("Please enter lastname"); return false; } return true; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started