jaimex8
12/9/2015 - 10:53 PM

Update price on attribute change

Update price on attribute change

// Begin Price Javascript
$(document).ready(function() {

  var ProductPrice = &mvt:product:price;;
  var AttributeCost;

  $('.AttributeDropdown,.AttributeRadio,.AttributeCheckbox').change(function(){
    ComputeTotalTemp();
  });

  <mvt:if expr="l.settings:product:price NE 0">
  ComputeTotalTemp();
  </mvt:if>
  
//console logs for debugging
  function ComputeTotalTemp(){
    TotalAttributeCost = 0;
    $('.CalculatedAttribute').each(function(){
        if ( $(this).is('select') ){
          //console.log('isSelect');
          AttributeCost = $("option:selected", this).attr("AttribCost");
          //console.log('initialPriceIs'+AttributeCost);
          TotalAttributeCost += Number(AttributeCost);
          //console.log('secondPriceIs'+ AttributeCost);
        }else if ( $(this).is('input:checkbox:checked') || $(this).is('input:radio:checked')){
          //console.log('isCheck');
          AttributeCost = $(this).attr("AttribCost");
          //console.log('thirdPriceIs'+AttributeCost);
          TotalAttributeCost += Number(AttributeCost);
        };
    });
    //console.log(TotalAttributeCost);
    var totalCost = Number(ProductPrice) + Number(TotalAttributeCost);
    document.getElementById('attrProdTotal').innerHTML = formatCurrency(totalCost);
  };

  function formatCurrency(num) {
  num=num.toString().replace(/$|,/g,'');
  if(isNaN(num)){
  num="0";
  }
  sign=(num == (num=Math.abs(num)));
  num=Math.floor(num*100+0.50000000001);
  cents=num%100;
  num=Math.floor(num/100).toString();
  if(cents<10){
  cents="0" + cents;
  }
  for (var i=0; i < Math.floor((num.length-(1+i))/3); i++)
  {
  num=num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
  }
  return (((sign)?'':'-') + '$' + num + '.' + cents);
  }

});
// End Price Javascript