Skip to content

Instantly share code, notes, and snippets.

@emilian
Created November 2, 2011 10:47
Show Gist options
  • Save emilian/1333377 to your computer and use it in GitHub Desktop.
Save emilian/1333377 to your computer and use it in GitHub Desktop.
Product Page Price Calculator
<script type="text/javascript">
//<![CDATA[
var price = "%%GLOBAL_ProductPrice%%";
price = price.replace("$","");
price = price.replace(",","");
var pricei = parseInt(price);
var qty = $('#qty_').val();
var qtyi = parseFloat(qty);
var qtis = 0;
var v = "";
$(".ProductMain").ajaxSuccess(function(){
var price = $('.VariationProductPrice').text();
price = price.replace("$","");
price = price.replace(",","");
pricei = parseFloat(price);
var qty = $('#qty_').val();
var qtyi = parseInt(qty);
var qtyis = qtyi * pricei;
v = formatCurrency(qtyis);
$('.ProductPrice').text(v);
});
$('#qty_').change(function(){
var qty = $('#qty_').val();
var qtyi = parseInt(qty);
var qtyis = qtyi * pricei;
v = formatCurrency(qtyis);
$('.ProductPrice').text(v);
price = $('.VariationProductPrice').text();
price = price.replace("$","");
price = price.replace(",","");
pricei = parseFloat(price);
pricei = pricei / qtyi;
});
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);
}
//]]></script>
@MichaelSzabo
Copy link

Hi Emilian.

Thanks for the script. While debugging I noticed that line 7 should use parseFloat otherwise prices of products will be rounded. Line 9 should also be using parseInt.

Also do you have any Ideas on how to incorporate bulk pricing discounts into live pricing?

Michael

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment