www.webdeveloper.com
Results 1 to 2 of 2

Thread: Newbie building a simple shopping cart

  1. #1
    Join Date
    Oct 2009
    Posts
    2

    Question 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 www.thepole.fi/order.html (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 http://www.mcfedries.com/JavaScript/OrderTotals.asp

    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 (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.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!

  2. #2
    Join Date
    Oct 2009
    Posts
    2
    I've found the solution!

    Thanks for anyone reading

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles