JavaScript – Why Can’t I Discount the Price?

I have a problem with my javascript. If my cart quantity is between 5-10 products then 5% discount will be given on total price. If more than 10, it will take 15%. Now my JS gets discount of only 10 products, although 3 products have been received. Can anyone help me>

 function getOrderInfo(){
    name = document.getElementById('name').value;
    phone = document.getElementById('phone').value;
    address = document.getElementById('address').value;

    let total =localStorage.getItem('total');
    let cartItems = localStorage.getItem('productsInCart');

    document.getElementById('summaryBtn').innerHTML = "Order Summary for " + localStorage.getItem('name');
    document.getElementById('summary_name').innerHTML = "Name: " + localStorage.getItem('name');
    document.getElementById('summary_phone').innerHTML = "Contact: " + localStorage.getItem('phone');
    document.getElementById('summary_address').innerHTML = "Address: " + localStorage.getItem('address');
    


    let grandtotal = total.replace('$','');
    grandtotal = grandtotal.replace(',','');

    let finalPrice = 0
    let fee = 0;
    if(cartItems.length >= 5 && cartItems.length <= 10){
        finalPrice = Number(0.95) * parseFloat(grandtotal);
        if(finalPrice > 100){
            fee = 0;
        }else{
            fee = 10;
        }
        document.getElementsByClassName('basketTotalTitle')[0].innerHTML = "Discount 5%";
    }
    else if(cartItems.length > 10){
        finalPrice = Number(0.85) * parseFloat(grandtotal);
        if(finalPrice > 100){
            fee = 0;
        }else{
            fee = 10;
        }
        document.getElementsByClassName('basketTotalTitle')[0].innerHTML = "Basket Total:" + total + " <br> Discount: 15% <br> Postage: $" + fee + "<br>";
    }
    else{
        finalPrice= parseFloat(grandtotal);
        if(finalPrice > 100){
            fee = 0;
        }else{
            fee = 10;
        }
        document.getElementsByClassName('basketTotalTitle')[0].innerHTML = "Discount 0%";
    }

    document.getElementById('total').innerHTML = "Final Price: $" + finalPrice;
}

Write a Comment

Your email address will not be published. Required fields are marked *