Search code examples
javascriptjquerycalculatorpromotion-code

Trying to add promo code functionality to discount number


Try adding a calculator function for a promo code.

Demo code: http://apollo.us/promo/

JS code: http://apollo.us/promo/js/webEstimator.js

When you write promo code - test or test1 - final TOTAL price change.

The final total price is displayed on the : var $total = $(".W_E-total"); // show total price

Promocode function:

var $finalprice = $('.W_E-total').val();
var promocode;

$('#update').click(function() {
  promocode = $('#promocode').val();
  total = $('.W_E-total').val();

  finalprice = total;
  if ((promocode == 'test') || (promocode == 'test1')) {
   finalprice = +finalprice * 0.9;
  } else if (promocode.length < 1) {
   finalprice = +finalprice * 1;
  } else {
   alert("Invalid Promo Code");
   finalprice = 0;
  }
  $('.W_E-total').val(finalprice);
}); 

You pressing the button "Update TOTAL Price" - TOTAL be changed.

What needs to change in the code?

Thank you! Happy Holidays!


Solution

  • This should do it:

    var $total = $("#W_E-total"); // show total price
    
    /* PROMO CODE */
    var max_price = parseInt($('#W_E-total').val()),
        finalprice = max_price;
    var promocode;
    
    $('#update').click(function () {
        promocode = $('#promocode').val();
    
        if ((promocode == 'test') || (promocode == 'test1')) {
            finalprice = max_price * 0.9;
        } else if (promocode.length < 1) {
            finalprice = max_price;
        } else {
            alert("Invalid Promo Code");
            finalprice = 0;  //Shouldn't this be maxprice too?
        }
        $total.val(finalprice);
    });