• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 16. Case Study: Online Store > Finalizing the Shopping Cart Contents

Finalizing the Shopping Cart Contents

When the customer chooses to begin the checkout process, you need to display the contents of the shopping cart and allow the customer to make any last-minute changes before completing the purchase. The script to do this, checkout.cfm, is shown in Listing 16.5.

Listing 16.5. checkout.cfm—Displays the Contents of the Shopping Cart and Allows Modification of Quantities

<!---

    Online Store Case Study

    This script displays the contents of the shopping
    cart for the user. First check to see if a form
    field called continue exists. If it does, send
    the user on to the next step in the checkout
    process (customerinfo.cfm).

--->

<cfif isDefined("Form.continue")>

    <cflocation url="customerinfo.cfm">

</cfif>

<!---

    Next check to see if the user clicked the
    submit button called update. If so, loop
    through the form fields called qty1, qty2,
    qty3, etc. and compare each form field value
    to the quantity stored in Session.arrQuantities.

    If the form field value is zero, this means
    that the user wants the product removed from
    the shopping cart, so add the value of the loop
    index i to a list called deleteList. The
    deletes will happen later so as not to
    negatively impact the <cfloop> which counts
    from 1 up to the number of elements in the
    array Session.arrProducts.

    If the form field value is not zero and
    different from the quantity stored in the
    array Session.arrQuantities, then update
    Session.arrQuantities to reflect the
    changed quantity.

--->

<cfif isDefined("Form.update")>
    <cfset deleteList = "">

    <cflock scope="session" type="exclusive" timeout="5">

        <cfloop from="1" to="#ArrayLen(Session.arrProducts)#" index="i">

            <cfif Evaluate("Form.qty" & i) eq 0>

                <cfset deleteList = ListAppend(deleteList,i)>

            <cfelseif Evaluate("Form.qty" & i) neq Session.arrQuantities[i]>

                <cfset Session.arrQuantities[i] = Evaluate("Form.qty" & i)>

            </cfif>

        </cfloop>

        <!---

            If there's anything in deleteList, loop over that
            list to delete the products and corresponding
            quantities that the user wants removed from the
            shopping cart.

        --->

        <cfif ListLen(deleteList)>

            <cfloop list="#deleteList#" index="i">

                <cfset temp = ArrayDeleteAt(Session.arrProducts,i)>
                <cfset temp = ArrayDeleteAt(Session.arrQuantities,i)>

            </cfloop>

        </cfif>

    </cflock>

</cfif>

<!---

    Initialize a variable called total that will be used
    to add up the total cost of the order.

--->

<cfset total = 0>

<html>

<head>
<title>Checkout Counter</title>
</head>

<body>

<h1>Checkout Counter</h1>

<h2>Step 1 of 3: Finalize Your Cart</h2>

<p>Your purchases are summarized below. If you would like to change
any quantities, please do so on the form below and click the "Update
Cart" button. To remove an item from your shopping cart, change its
quantity to zero. If you are satisfied with the contents of your
shopping cart, then please click the "Continue Checkout" button.</p>

<!---

    <cfform> and <cfinput> incorporate JavaScript into
    the finished page that checks the quantity fields
    to make sure they have valid values.

--->

<cfform action="checkout.cfm" method="post">

<table cellpadding="6" border="1">

<tr>
<th>Product ID</th>
<th>Product Name</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Extended Price</th>
</tr>

<!---

    Loop over the arrays Session.arrProducts and
    Session.arrQuantities to generate the rows in
    the summary table. The product's name and price
    need to be retrieved from the database in each
    iteration of the loop.

    The extended price is equal to the unit price
    times the quantity purchased.

--->

<cflock scope="session" type="readonly" timeout="5">

<cfloop from="1" to="#ArrayLen(Session.arrProducts)#" index="i">

    <cfquery datasource="#dsn#" name="qGetProductInfo">
    SELECT ProductName, ProductPrice
    FROM Products
    WHERE ProductID = #Session.arrProducts[i]#
    </cfquery>

    <cfoutput>
    <tr>
    <td align="center">#Session.arrProducts[i]#</td>
    <td>#qGetProductInfo.ProductName#</td>
    <td align="center"><cfinput type="text" name="qty#i#" size="3"
        value="#Session.arrQuantities[i]#" required="yes"
        validate="integer" message="Please enter a valid quantity for
        Product ID #Session.arrProducts[i]#"></td>
    <td align="right">#DollarFormat(qGetProductInfo.ProductPrice)#</td>
    <td align="right">#DollarFormat(Session.arrQuantities[i] *
        qGetProductInfo.ProductPrice)#</td>
    </tr>
    </cfoutput>

    <!---

        Update the running total

    --->

    <cfset total = total + Session.arrQuantities[i] *
        qGetProductInfo.ProductPrice>

</cfloop>

    <!---

        Once the loop is over, store the total in a session
        variable so it does not have to be recomputed later
        in the checkout process.

    --->

    <cfset Session.total = total>

</cflock>

    <!---

        Put a final row on the table that displays the
        cost of the entire order.

    --->

    <tr align="right">
    <td colspan="4"><b>Grand Total:</b></td>
    <td><b><cfoutput>#DollarFormat(total)#</cfoutput></b></td>
    </tr>

</table>

<br><br>

<input type="submit" value="Update Cart" name="update">
&nbsp;&nbsp;
<input type="submit" value="Continue Checkout" name="continue">

</cfform>

</body>

</html>


					  


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint