// This function will analyse the contents of the pricing form and will calculate the // appropriate price shown next to the add to basket option this will show the actual // price of all good being added rather than the individual costs elements function dynamic_pricing_calc_css(form, $currency_symbol) { // start by working out wether or not we are working with a single item or // wether there are multiple options we can check to see if a control is an array var total_cost = 0; var total_discount = 0; var quantity = 0; var unit_cost = 0; var line_cost = 0; // This is a global variable defined in core_system.js and can be used to alert the user when they are about // to navigate away before saving / commiting any changes they have made user_changes = true; // Check for no-option standard product pricing if (form.product_quantity) { // This is just a straight product pricing option total_cost = (form.product_quantity.value * form.product_cost.value); // Check to see if there are any QPB values for this item var quantity = form.product_quantity.value; var quantity_name = 'qpb_quantity_' + form.primary_product_id.value; var costs_name = 'qpb_value_' + form.product_id.value; var div_name = 'item_price_' + form.product_id.value; var item_price_div = document.getElementById(div_name); var break_quantities = document.getElementsByName(quantity_name); var break_values = document.getElementsByName(costs_name); // Handle both multiple and single breaks form.product_cost.value = form.product_original_cost.value; if (break_quantities.length) { for (var i=0; i < break_quantities.length; i++) { var check_quantity = break_quantities[i].value; if (parseInt(quantity) >= parseInt(check_quantity)) { form.product_cost.value = break_values[i].value; } } } else { // Single break value if (quantity >= break_quantities.value) { form.product_cost.value = break_values.value; } } // Update the item price if possible if (item_price_div) { var item_value = form.product_cost.value; item_price_div.innerHTML = $currency_symbol + parseFloat(item_value).toFixed(2); } // Now we can calculate the line total total_cost = quantity * form.product_cost.value; total_discount = quantity * (form.product_standard_price.value - form.product_cost.value); if (total_discount < 0) total_discount = 0; } else { // Check to see if there are multiple options available quantities = document.getElementsByName('variation_quantity[]'); product_ids = document.getElementsByName('variation_product_id[]'); product_costs = document.getElementsByName('variation_cost[]'); product_original_costs = document.getElementsByName('variation_original_cost[]'); standard_costs = document.getElementsByName('variation_standard_price[]'); if (quantities.length) { for (var n=0; n < quantities.length; n++) { // Check to see if there are any QPB values for this item var quantity = quantities[n].value; var product_id = product_ids[n].value; var quantity_name = 'qpb_quantity_' + product_id + '[]'; var costs_name = 'qpb_value_' + product_id + '[]'; var div_name = 'item_price_' + product_id; var item_price_div = document.getElementById(div_name); var break_quantities = document.getElementsByName(quantity_name); var break_values = document.getElementsByName(costs_name); // Handle both multiple and single breaks product_costs[n].value = product_original_costs[n].value; if (break_quantities.length) { for (var i=0; i < break_quantities.length; i++) { var check_quantity = break_quantities[i].value; if (parseInt(quantity) >= parseInt(check_quantity)) { product_costs[n].value = break_values[i].value; } } } else { // Single break value if (quantity >= break_quantities.value) { product_costs[n].value = break_values.value; } } // Update the item price if possible if (item_price_div) { var item_value = product_costs[n].value; item_price_div.innerHTML = $currency_symbol + parseFloat(item_value).toFixed(2); } // Now we can calculate the line total line_cost = quantity * product_costs[n].value; line_discount = quantity * (standard_costs[n].value - product_costs[n].value); if (line_discount < 0) line_discount = 0; total_cost = total_cost + line_cost; total_discount = total_discount + line_discount; } } else { // Check to see if there are any QPB values for this item var quantity = form.variation_quantity.value; var quantity_name = 'qpb_quantity_' + form.variation_product_id.value + '[]'; var costs_name = 'qpb_value_' + form.variation_product_id.value + '[]'; var div_name = 'item_price_' + form.variation_product_id.value; var item_price_div = document.getElementById(div_name); var break_quantities = document.getElementsByName(quantity_name); var break_values = document.getElementsByName(costs_name); // Handle both multiple and single breaks form.variation_cost.value = form.variation_orginal_cost.value; if (break_quantities.length) { for (var i=0; i < break_quantities.length; i++) { var check_quantity = break_quantities[i].value; if (parseInt(quantity) >= parseInt(check_quantity)) { form.variation_cost.value = break_values[i].value; } } } else { // Single break value if (quantity >= break_quantities.value) { form.variation_cost.value = break_values.value; } } // Update the item price if possible if (item_price_div) { var item_value = form.variation_cost.value; item_price_div.innerHTML = $currency_symbol + parseFloat(item_value).toFixed(2); } total_cost = (form.variation_quantity.value * form.variation_cost.value); total_discount = form.variation_quantity.value * (form.variation_standard_price.value - form.variation_cost.value); } } // Sanity check the discount if (total_discount < 0) total_discount = 0; // Check to make sure we can find the element on the form we need to write back to discount_div = document.getElementById('dynamic_cost_calc_discount'); output_div = document.getElementById('dynamic_cost_calc_total'); if (output_div) { output_div.innerHTML = 'Cost: ' + formatCurrency(total_cost, $currency_symbol); } if (discount_div) { if (total_discount > 0.01) discount_div.style.display = ''; else discount_div.style.display = 'none'; discount_div.innerHTML = 'Online Saving: ' + formatCurrency(total_discount, $currency_symbol); } } // This function will analyse the contents of the pricing form and will calculate the // appropriate price shown next to the add to basket option this will show the actual // price of all good being added rather than the individual costs elements function dynamic_pricing_calc(form, $currency_symbol) { // start by working out wether or not we are working with a single item or // wether there are multiple options we can check to see if a control is an array var total_cost = 0; var total_discount = 0; var quantity = 0; var unit_cost = 0; var line_cost = 0; // This is a global variable defined in core_system.js and can be used to alert the user when they are about // to navigate away before saving / commiting any changes they have made user_changes = true; // Check for no-option standard product pricing if (form.product_quantity) { // This is just a straight product pricing option total_cost = (form.product_quantity.value * form.product_cost.value); // Check to see if there are any QPB values for this item var quantity = form.product_quantity.value; var quantity_name = 'qpb_quantity_' + form.primary_product_id.value; var costs_name = 'qpb_value_' + form.product_id.value; var div_name = 'item_price_' + form.product_id.value; var item_price_div = document.getElementById(div_name); var break_quantities = document.getElementsByName(quantity_name); var break_values = document.getElementsByName(costs_name); // Handle both multiple and single breaks form.product_cost.value = form.product_original_cost.value; if (break_quantities.length) { for (var i=0; i < break_quantities.length; i++) { var check_quantity = break_quantities[i].value; if (parseInt(quantity) >= parseInt(check_quantity)) { form.product_cost.value = break_values[i].value; } } } else { // Single break value if (quantity >= break_quantities.value) { form.product_cost.value = break_values.value; } } // Update the item price if possible if (item_price_div) { var item_value = form.product_cost.value; item_price_div.innerHTML = $currency_symbol + parseFloat(item_value).toFixed(2); } // Now we can calculate the line total total_cost = quantity * form.product_cost.value; total_discount = quantity * (form.product_standard_price.value - form.product_cost.value); if (total_discount < 0) total_discount = 0; } else { // Check to see if there are multiple options available if (form.variation_quantity.length) { for (var n=0; n < form.variation_quantity.length; n++) { // Check to see if there are any QPB values for this item var quantity = form.variation_quantity[n].value; var quantity_name = 'qpb_quantity_' + form.variation_product_id[n].value + '[]'; var costs_name = 'qpb_value_' + form.variation_product_id[n].value + '[]'; var div_name = 'item_price_' + form.variation_product_id[n].value; var item_price_div = document.getElementById(div_name); var break_quantities = document.getElementsByName(quantity_name); var break_values = document.getElementsByName(costs_name); // Handle both multiple and single breaks form.variation_cost[n].value = form.variation_orginal_cost[n].value; if (break_quantities.length) { for (var i=0; i < break_quantities.length; i++) { var check_quantity = break_quantities[i].value; if (parseInt(quantity) >= parseInt(check_quantity)) { form.variation_cost[n].value = break_values[i].value; } } } else { // Single break value if (quantity >= break_quantities.value) { form.variation_cost[n].value = break_values.value; } } // Update the item price if possible if (item_price_div) { var item_value = form.variation_cost[n].value; item_price_div.innerHTML = $currency_symbol + parseFloat(item_value).toFixed(2); } // Now we can calculate the line total line_cost = quantity * form.variation_cost[n].value; line_discount = quantity * (form.variation_standard_price[n].value - form.variation_cost[n].value); if (line_discount < 0) line_discount = 0; total_cost = total_cost + line_cost; total_discount = total_discount + line_discount; } } else { // Check to see if there are any QPB values for this item var quantity = form.variation_quantity.value; var quantity_name = 'qpb_quantity_' + form.variation_product_id.value + '[]'; var costs_name = 'qpb_value_' + form.variation_product_id.value + '[]'; var div_name = 'item_price_' + form.variation_product_id.value; var item_price_div = document.getElementById(div_name); var break_quantities = document.getElementsByName(quantity_name); var break_values = document.getElementsByName(costs_name); // Handle both multiple and single breaks form.variation_cost.value = form.variation_orginal_cost.value; if (break_quantities.length) { for (var i=0; i < break_quantities.length; i++) { var check_quantity = break_quantities[i].value; if (parseInt(quantity) >= parseInt(check_quantity)) { form.variation_cost.value = break_values[i].value; } } } else { // Single break value if (quantity >= break_quantities.value) { form.variation_cost.value = break_values.value; } } // Update the item price if possible if (item_price_div) { var item_value = form.variation_cost.value; item_price_div.innerHTML = $currency_symbol + parseFloat(item_value).toFixed(2); } total_cost = (form.variation_quantity.value * form.variation_cost.value); total_discount = form.variation_quantity.value * (form.variation_standard_price.value - form.variation_cost.value); } } // Sanity check the discount if (total_discount < 0) total_discount = 0; // Check to make sure we can find the element on the form we need to write back to discount_div = document.getElementById('dynamic_cost_calc_discount'); output_div = document.getElementById('dynamic_cost_calc_total'); if (output_div) { output_div.innerHTML = 'Cost: ' + formatCurrency(total_cost, $currency_symbol); } if (discount_div) { discount_div.innerHTML = 'Online Saving: ' + formatCurrency(total_discount, $currency_symbol); } } // This function will format the specified value into a suitable format for the screen function formatCurrency(num, $currency_symbol) { // Clean up the number string num = num.toString().replace(/\|\,/g,''); // Default to zero if invalid 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)?'':'-') + $currency_symbol + num + '.' + cents); } // This function will validate a standard pricing panel to ensure all of the // relevant information has been provided to ensure sensible data is sent to // the server in the future function product_add_validation(form) { // Initialise the variables var error = 0; var error_message = "PLEASE COMPLETE THE FOLLOWING\n\n"; var quantity = 0; // Check for no-option standard product pricing if (form.product_quantity) { // This is just a straight product pricing option quantity = form.product_quantity.value; } else { quantities = $( form ).getElements( 'input[name^=variation_quantity]' ); quantities.each( function( variation_quantity ) { quantity = quantity + variation_quantity.value; }); } // Make sure we have a quantity at this point if (quantity <= 0) { error = 1; error_message = "Please specify a valid quantity"; } // Check to make sure we can continue if (error == 1) { alert(error_message); return false; } else { return true; } }