Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Calculate selected attributes price in product_info to show total price


andy_1984

Recommended Posts

been looking high and low for this. as the title says. i would like to be able to re-calculate the price of the product when an attribute is selected without leaving product_info.php so if product is £10 and attribute is selected that costs an additional £10 the price shown would be £20. some sort of java or ajax ?

Link to comment
Share on other sites

------------------------------------------------------------------------------------------------------------

Manual install instructions (catalog/product_info.php)

------------------------------------------------------------------------------------------------------------

Search for:

<link rel="stylesheet" type="text/css" href="stylesheet.css">

 

After that line add:

<!-- Attrib price update  //-->
<script language="javascript">
<!--
 function calcPrice(el) {
   //el = <select> that is being changed and that contains the price details
   var currencySymbolLeft = '<?php echo htmlentities($currencies->currencies[DEFAULT_CURRENCY]['symbol_left']); ?>';
   var currencySymbolRight = '<?php echo htmlentities($currencies->currencies[DEFAULT_CURRENCY]['symbol_right']); ?>';
   var currencyDecimalPoint = '<?php echo htmlentities($currencies->currencies[DEFAULT_CURRENCY]['decimal_point']); ?>';
   var currencyThousandsPoint = '<?php echo htmlentities($currencies->currencies[DEFAULT_CURRENCY]['thousands_point']); ?>';
   var currencyDecimalPlaces = '<?php echo htmlentities($currencies->currencies[DEFAULT_CURRENCY]['decimal_places']); ?>';
   var itemBasePrice = parseFloat(document.getElementById('itemPrice').value);
   var itemBasePrice1 = parseFloat(document.getElementById('itemPrice1').value);
   var priceDiff = 0;
   for(f = 0; f < document.forms.length ; f++) {
	frm = document.forms[f];
	for(e = 0; e < frm.elements.length; e++) {
	elm = frm.elements[e];
	if(elm.type == "select-one" ) {
		var curSelItemText = elm.options[elm.selectedIndex].text;
		var posOpenBracket = curSelItemText.indexOf('(');
		var posCloseBracket = curSelItemText.indexOf(')');
		if (posOpenBracket != -1 && posCloseBracket != -1) {
			priceDiff += parseFloat(curSelItemText.substr(posOpenBracket + 1, posCloseBracket - posOpenBracket - 1).replace(currencySymbolLeft, '').replace(currencySymbolRight, '').replace(currencyThousandsPoint, ''));
		}
	}
     }
   }
   var totalPrice = itemBasePrice + priceDiff;
   var totalPrice1 = itemBasePrice1 + priceDiff;
   var finalFormattedPrice = currencySymbolLeft + number_format(totalPrice, currencyDecimalPlaces, currencyDecimalPoint, currencyThousandsPoint) + currencySymbolRight;
   var finalFormattedPrice1 = currencySymbolLeft + number_format(totalPrice1, currencyDecimalPlaces, currencyDecimalPoint, currencyThousandsPoint) + currencySymbolRight;
   document.getElementById("productNEWprice").innerHTML = finalFormattedPrice;
   if (document.getElementById("productNEWprice1") != undefined) {
     document.getElementById("productNEWprice1").innerHTML = finalFormattedPrice1;
   }
 }

 function number_format(number, decimals, dec_point, thousands_sep) {
   // http://phpjs.org/
   number = (number+'').replace(',', '').replace(' ', '');
   var n = !isFinite(+number) ? 0 : +number, 
     prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
     sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
     dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
     s = '',
     toFixedFix = function (n, prec) {
       var k = Math.pow(10, prec);
       return '' + Math.round(n * k) / k;
     };
   // Fix for IE parseFloat(0.55).toFixed(0) = 0;
   s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
   if (s[0].length > 3) {
     s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
   }
   if ((s[1] || '').length < prec) {
     s[1] = s[1] || '';
     s[1] += new Array(prec - s[1].length + 1).join('0');
   }
   return s.join(dec);
 }
//-->
</script>
<!-- Attrib price update Fin //-->

------------------------------------------------------------------------------------------------------------

Search for:

 

</body>

 

Add the following lines afterwards:

<!-- Attrib price update  //-->
<script language="javascript">
<!--
 calcPrice(document.getElementById('selectSize'));
//-->
</script>
<!-- Attrib price update Fin //-->

 

------------------------------------------------------------------------------------------------------------

Search for:

 

 <td class="pageHeading" align="right" valign="top"><?php echo $products_price; ?>

 

Replace it by.:

<td class="pageHeading" align="right" valign="top">
<!-- price update attributes begin -->
		<?php 
			if(tep_has_product_attributes($product_info['products_id'])){ 
				if ($new_price = tep_get_products_special_price($product_info['products_id'])) {
					?> 
					<script type="text/javascript">document.write('<s><div id="productNEWprice1"></div>\n</s>');</script>
					<script type="text/javascript">document.write('<span class="productSpecialPrice"><div id="productNEWprice"></div>\n</span>');</script>
					<noscript><?php echo $products_price; ?></noscript>
					<?php } else {?>
					<script type="text/javascript">document.write('<div id="productNEWprice"></div>\n</span>');</script>
					<noscript><?php echo $products_price; ?></noscript>
					<?php }
			 } else { 
				 echo $products_price; } ?>

           </td>
         <input type="hidden" name="nuPrice" id="itemPrice" value="<?php echo str_replace("$","",$nuPrice); ?>">
	  <input type="hidden" name="nuPrice1" id="itemPrice1" value="<?php echo str_replace("$","",$nuPrice1); ?>">
<!-- price update attributes end -->

 

------------------------------------------------------------------------------------------------------------

Search for:

 

 $products_price = '<s>' . $currencies->display_price($product_info['products_price'], tep_get_tax_rate($product_info['products_tax_class_id'])) . '</s> <span class="productSpecialPrice">' . $currencies->display_price($new_price, tep_get_tax_rate($product_info['products_tax_class_id'])) . '</span>';

After add:

//Attrib price update
$nuPrice1 =  $currencies->display_price($product_info['products_price'], tep_get_tax_rate($product_info['products_tax_class_id']));
$nuPrice =  $currencies->display_price($new_price, tep_get_tax_rate($product_info_values['products_tax_class_id']));
//Attrib price update Fin

 

------------------------------------------------------------------------------------------------------------

Search for:

$products_price = $currencies->display_price($product_info['products_price'], tep_get_tax_rate($product_info['products_tax_class_id']));
   }

 

Replace by:

//Attrib price update
$products_price = $currencies->display_price($product_info['products_price'], tep_get_tax_rate($product_info['products_tax_class_id']));
$nuPrice = $products_price;
   }
$nuPrice = str_replace(",", ".", $nuPrice);
$nuPrice1 = str_replace(",", ".", $nuPrice1);
//Attrib price update Fin

------------------------------------------------------------------------------------------------------------

Search For:

<td class="main"><?php echo tep_draw_pull_down_menu('id[' . $products_options_name['products_options_id']
   . ']', $products_options_array, $selected_attribute); ?></td>

Change to:

<!-- Attrib price update  //-->
<td class="main"><?php echo tep_draw_pull_down_menu('id[' . $products_options_name['products_options_id'] . ']', $products_options_array, $selected_attribute, "id="selectSize" onChange='calcPrice(this);'");?></td>
<!-- Proyecto Icaro (Fin Modificado) //-->

Link to comment
Share on other sites

I resolved the problem writing this in the last line:

 

<td class="main"><?php echo tep_draw_pull_down_menu('id[' . $ProdOpt_ID . ']', $products_options_array, $selected_attribute, 'onChange=calcPrice(this);') ?></td>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...