/    Sign up×
Community /Pin to ProfileBookmark

Newbie building a simple shopping cart

Hi,

I promised to help a friend of mine to build a simple shopping cart to his web-page. The current page can be found from [url]www.thepole.fi/order.html[/url] (yes, I’m 100% aware of the low level in the quality of the implementation).

I’ve so far managed to get the JS to count the total price for the products. For this I’m using script found from [url]http://www.mcfedries.com/JavaScript/OrderTotals.asp[/url]

Now I have 2 problems:
1) How do I get the shipping counted in to the total price? If the shipping destination is outside Finland there is either 6€ or 12€ added fee per book. The user can select the destination using radio buttons.

2) Currently total amount is displayed in the textfield with name “Total”. How do I get the value multiplied with 100, and displayed in the textfield named “Amount”? The reason for this is that when the form is submitted to the credit card company, the amount of money should be in hidden “Amount”-textfield, and the money unit should be cents (1 euro in the Total-field should be 100 cents in the-Amount field)

The script I’m using goes as follows:

<script language=”JavaScript” type=”text/javascript”>
<!–

/* This script is Copyright (c) Paul McFedries and
Logophilia Limited ([url]http://www.mcfedries.com/[/url]).
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.length; ++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.substring(0,4) == “PROD”) {

// If so, extract the price from the name
item_price = parseFloat(form_name.substring(form_name.lastIndexOf(“_”) + 1))

// Get the quantity
item_quantity = parseInt(form_field.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, 0)

}

function round_decimals(original_number, decimals) {
var result1 = original_number * Math.pow(10, decimals)
var result2 = Math.round(result1)
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.toString()

// Locate the decimal point
var decimal_location = value_string.indexOf(“.”)

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

// If no, then all decimal places will be padded with 0s
decimal_part_length = 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_length = value_string.length – decimal_location – 1
}

// Calculate the number of decimal places that need to be padded with 0s
var pad_total = decimal_places – decimal_part_length

if (pad_total > 0) {

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

}

//–>
</script>

The product is added with this

<INPUT TYPE=TEXT NAME=”PROD_SP_46″ onChange=”CalculateTotal(this.form)”>

And the total is calculated with this
<input type=TEXT name=Total onFocus=”this.form.elements[0].focus()”>

I didn’t find any forum rules that would forbid me from offering a reward from helping (maybe common courtesy – but I really need this done so please forgive me), so I’m willing to donate 20€ for the person solving my problem.

Thanks for the help in advance!

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@MoosexauthorOct 09.2009 — I've found the solution!

Thanks for anyone reading ?
×

Success!

Help @Moosex spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...