SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Location
    Houston, TX
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Subtracting discount on order form?

    Hello,

    I have a custom order form that will add up and show the total but I'm having trouble getting it to subtract the discount. What changes do I need to make to my javascript?

    The form is here:
    http://www.houston-reviews.com/purchase/order.php

    This is the javascript I'm using which is also viewable in the source code of the above form:

    Code JavaScript:
    <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
    //	var order_disc = 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 "discount" field?
            if (form_name.substring(0,4) == "DISC") {
     
                // If so, extract the price from the name
                item_price2 = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))
     
                // Get the quantity
                item_quantity2 = parseInt(form_field.value)
     
                // Update the order total
                if (item_quantity2 >= 0) {
    				var order_disc
                   order_disc += item_quantity2 * item_price2
    				//alert(order_disc)
                }
            }
     
    		*/
     
     
     
     
     
            // 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, 2)
    	frm.amount.value = round_decimals(order_total, 2)
     
    }
     
    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>

    Thanks in advance!

    Tim

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Where in the form is the discount specified? Are they the SuppYo credits?

    If so, the field name would need to start with DISC for the script to interpret it appropriately, due to this:
    Code:
    if (form_name.substring(0,4) == "DISC") {
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Location
    Houston, TX
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Yes, the discount being applied is the Suppyo Credits. The if loop with the "DISC" is a loop I added to the javascript. I also had renamed the field to DISC. It didn't work so I commented it out and changed the field name back.

    The DISC loop isn't working. Any ideas on how to accomplish the subtraction of the Suppyo Credit?

    Thanks for your input,

    Tim

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Tim Mousel View Post
    The if loop with the "DISC" is a loop I added to the javascript. I also had renamed the field to DISC. It didn't work so I commented it out and changed the field name back.

    The DISC loop isn't working. Any ideas on how to accomplish the subtraction of the Suppyo Credit?
    I suggest that you uncomment the discount code and rename the field again, so that we can look investigate the page and determine what more will be needed to get things working properly.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Location
    Houston, TX
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I uncommented the discount code and renamed the field name.
    http://www.houston-reviews.com/purchase/order.php

    Thanks,

    Tim

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Here is how your existing discount code gets the order_disc value:

    Code javascript:
    // If so, extract the price from the name
    item_price2 = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))
    // Get the quantity
    item_quantity2 = parseInt(form_field.value)
    // Update the order total
    if (item_quantity2 >= 0) {
        var order_disc
        order_disc += item_quantity2 * item_price2
    }

    You don't need to bother with the price or quantity. You can get the discount amount straight from the select field, replacing all of the above with:

    Code javascript:
    // If so, extract the price from the select field
    order_disc += Number(form_field.value);
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Location
    Houston, TX
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for your help Paul!

    I made the change you suggested but the total value doesn't update?

    Thanks,

    Tim

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Tim Mousel View Post
    I made the change you suggested but the total value doesn't update?
    I'm not testing the code locally elsewhere, so we're going to go through this step by step on your own server so that you know what's happening with troubleshooting this too.

    It looks like the code requires the discount to be placed before the item quantity.

    You can either update the HTML form so that the discount occurs before the quantity, or you can do the more time consuming task of updating the script, so that the order of things is less important.

    Which one will you do?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Enthusiast
    Join Date
    Aug 2005
    Location
    Houston, TX
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent! I changed the order in the html form and now it's working. I know PHP but not javascript but I'll try and figure out how to edit the script so the order doesn't matter.

    Thank you so much!!! I've spent many hours on this!

    Tim


Bookmarks

Posting Permissions

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