Community Add-Ons

Maintainers: imtiaz
Add file to this package
Top » Features

Ajax Price Update
for osCommerce Online Merchant v2.3

This module will update product price asynchronously (using AJAX) upon changing product attribute drop down.

Legend:  Download   Report

Expand All / Collapse All

Two quick fixes dreaminggates 28 Nov 2013  

***Please double-check this! It works fine on my cart but...***

This is only the javascript that goes at the end of product_info.php. Download the entire package from history below! And thank you to all who work hard on these add-ons <3

v.1.2.3, two tiny fixes:

1.) Subtraction problem - Negative amounts in American dollars were adding to the total, instead of subtracting.

2.) Sanity check - while testing, I found if options somehow end up with a negative total price, that would be passed as a negative amount to the shopping cart. Oops! So instead, I added a new variable, a "total display," which goes only as low as 0 (but keeps the math behind the scenes accurate).





<!-- v. 1.2.3 - includes fixes for negative amounts with US $ -->
<script type="text/javascript">
var actualprice = document.getElementById('productPrice').innerHTML;
var actualInd = actualprice.indexOf("(");
var price = actualprice.substring(actualInd+1, (actualprice.length));
var total =price.match(/[+-]*(d+).(d+)/)[0];
function changePrice(id) {
var select_list_field = document.getElementById(id);
var select_list_selected_index = select_list_field.selectedIndex;
var text = select_list_field.options[select_list_selected_index].text;

var ind = text.indexOf("(")+1;
var str = text.substring(ind, text.indexOf(")"));
if( str!='' ) {
str = str.replace(',',''); //Removes comma - added in version 1.2.1
str = str.replace('$',''); //Removes $ - added in version 1.2.3
str=str.match(/[+-]*(d+).(d+)/)[0];
};
var hFieldId = "hid_"+id;
var hiddenField = document.getElementById(hFieldId).value;
if(str=="select")
{
if( isNaN(hiddenField) )
{ } else {
total = total - hiddenField;
};
document.getElementById(hFieldId).value = 0;
} else {
document.getElementById(hFieldId).value = str;
if(hiddenField>0 || hiddenField<0)
{
total = total - hiddenField;
};
if( isNaN(str) || (str.length === 0) )
{
total = parseFloat(total);
}
else
{
total = parseFloat(str) + parseFloat(total);
};
// added v.1.2.3 - sanity check - keeps total from showing negative
var $total_display = 0;
if (total<0) {total_display=0;} else {total_display = total;}
}
// document.getElementById('productPrice').innerHTML = actualprice.replace(/[+-]*(d+).(d+)/, total.toFixed(2)); //Version 1.2.0
// document.getElementById('productPrice').innerHTML = actualprice.replace(/[+-]*(d+).(d+)/, total.toFixed(2).replace(/(d)(?=(d{3})+.)/g, "$1,")); //Version 1.2.1
document.getElementById('productPrice').innerHTML = actualprice.replace(/[+-]*(d+).(d+)/, total_display.toFixed(2).replace(/(d)(?=(d{3})+.)/g, "$1,")); //Version 1.2.3
}
</script>

Ajax Price Update v1.2.1 NimaP 25 Nov 2013  
Ajax Price Update v1.2 pedro.antunes 21 Jan 2013  
Ajax Price Update v1.1 modem2.0 10 Nov 2012  
Ajax Price Update imtiaz 22 Dec 2011