473,586 Members | 2,682 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

add shipping to order total script?

I'm trying to modify this script...

<script language="JavaS cript" type="text/javascript">
<!--

/* This script is Copyright (c) Paul McFedries and
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as
this Copyright notice remains in place.*/

function CalculateTotal( frm) {
var order_total = 0

// Run through all the form fields
for (var i=0; i < frm.elements.le ngth; ++i) {

// Get the current field
form_field = frm.elements[i]

// Get the field's name
form_name = form_field.name

// Is it a "product" field?
if (form_name.subs tring(0,4) == "PROD") {

// If so, extract the price from the name
item_price =
parseFloat(form _name.substring (form_name.last IndexOf("_") + 1))

// Get the quantity
item_quantity = parseInt(form_f ield.value)

// Update the order total
if (item_quantity >= 0) {
order_total += item_quantity * item_price
}
}
}

// Display the total rounded to two decimal places
frm.TOTAL.value = round_decimals( order_total, 2)
}

function round_decimals( original_number , decimals) {
var result1 = original_number * Math.pow(10, decimals)
var result2 = Math.round(resu lt1)
var result3 = result2 / Math.pow(10, decimals)
return pad_with_zeros( result3, decimals)
}

function pad_with_zeros( rounded_value, decimal_places) {

// Convert the number to a string
var value_string = rounded_value.t oString()

// Locate the decimal point
var decimal_locatio n = value_string.in dexOf(".")

// Is there a decimal point?
if (decimal_locati on == -1) {

// If no, then all decimal places will be padded with 0s
decimal_part_le ngth = 0

// If decimal_places is greater than zero, tack on a decimal
point
value_string += decimal_places > 0 ? "." : ""
}
else {

// If yes, then only the extra decimal places will be padded
with 0s
decimal_part_le ngth = value_string.le ngth - decimal_locatio n -
1
}

// Calculate the number of decimal places that need to be padded
with 0s
var pad_total = decimal_places - decimal_part_le ngth

if (pad_total > 0) {

// Pad the string with 0s
for (var counter = 1; counter <= pad_total; counter++)
value_string += "0"
}
return value_string
}

//-->
</script>

....so that it will provide a shipping cost based on the order total it
calculates.

I tried this approach...

// Update the order total
if (item_quantity >= 0) {
order_total += item_quantity * item_price
}
if (order_total > 1) {
ship_cost = 1
}
}
}

// Display the total rounded to two decimal places
frm.TOTAL.value = round_decimals( order_total, 2)
frm.SHIPPING.va lue = ship_cost
}

....and it works, but throws an error.

Jan 21 '06 #1
1 1903
JRS: In article <11************ **********@f14g 2000cwb.googleg roups.com>
, dated Sat, 21 Jan 2006 09:41:15 remote, seen in
news:comp.lang. javascript, we*******@yahoo .com posted :
I'm trying to modify this script...

<script language="JavaS cript" type="text/javascript">
<!--

/* This script is Copyright (c) Paul McFedries and
Logophilia Limited (http://www.mcfedries.com/).
Permission is granted to use this script as long as
this Copyright notice remains in place.*/


It's Copyright : you have permission to use it, but AFAICS not to
publish it (as you have done) or alter it.

Those considerations apart, it's not code that I would choose to use.

Those posting code to news should not allow their posting agent to wrap
it; that alienates potential readers.

Read the newsgroup FAQ.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jan 22 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4255
by: Howard Martin | last post by:
I have a form at http://www.develop.check.com.au/hh/order.html where an extended sub-total and then a grand total is calculated as soon as a quantity is input by a user. I would like to be able to add the shipping cost to that grand total. Shipping costs are in the "Postage & handling charges" link and are a cost per box. If somebody...
0
1275
by: ppatel | last post by:
In shopping cart I have a Product Quantity and Unitcost per product. Should I calculate shipping (per item weight * Quantity) for each item or should I add up total weight and calculate shipping rate. See my examples, e.g (1) ProductID Quantity Weight(per item) subtotal calculate Shipping rate 1 3 0.5 lb 1.5 lb $ 1.30 (e.g) 2 2 1 lb 2...
6
4616
by: Chris Buckett | last post by:
Hi, I was just wondering if anyone has had any experience in developing a shipping matrix. We are based in the UK, and we need to ship both locally and internationally. Local shipping is priced by weight and doesn't matter about the number of packages, international shipping is by weight and package size, so we need to know the number...
3
6269
by: libsfan01 | last post by:
Hi all! Can anyone show me how to use javascript to populate shipping address form fields with pre-typed payment address details upon checking of a checkbox next to the obligatory text "click here if the shipping address is the same as the billling address"? I've looked everywhere for it but surprizingly have had no luck!
16
5648
by: Brian Tkatch | last post by:
Is there a way to check the order in which SET INTEGRITY needs to be applied? This would be for a script with a dynamic list of TABLEs. B.
0
1391
by: tdxpress | last post by:
Hello, I need help creating a script to use in an Acrobat PDF fillable form. It's a simple form to order one particular item. However, the buyer may purchase up to 9 of this item on this order form. I would like the form to automatically calculate the shipping to be $10 for 1 ordered, an additional $5.00 for 2-8 qty, and free shipping for a...
1
2415
by: laredotornado | last post by:
Hi, Can anyone recommend a free script for calculating UPS shipping? I am familiar with the script written in 2000 by Jason Costomiris, but UPS is using an XML interface and I wondered if there's anything more updated. The pages I located on Google turned up broken links. Thanks, - Dave
1
1407
by: ajcolburn | last post by:
Hi there, I'm writing a very simple shopping cart for a friend, but I'm confusing myself with the SQL required to return postage costs for each product in the cart, at checkout. It's probably best to show you my tables first, each postage rate can apply to multiple products, product ranges, and countries. For example, postage rate X could...
0
7912
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5390
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3837
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1449
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.