Quantcast
Channel: Adobe Community : Unanswered Discussions - ColdFusion
Viewing all articles
Browse latest Browse all 5794

Extremely Simple Shopping Cart

$
0
0

Hi everyone. I was able to get a very simple shopping cart set up and to work with adding a Product to the cart and deleting the Product, but how do I add or display something such as the price of the product too? Here's what I have below on a products.cfm page and a ShoppingCart.cfm page:

 

products.cfm

<form action="ShoppingCart.cfm" method="post">

Product 1 Description

<input type="hidden" name="productID" value="1" />

<input type="hidden" name="productName" value="Socket" />

<input type="submit" value="Add To Cart" />

</form>

 

<form action="ShoppingCart.cfm" method="post">

Product 2 Description

<input type="hidden" name="productID" value="2" />

<input type="hidden" name="productName" value="Adapter" />

<input type="submit" value="Add To Cart" />

</form>

 

 

ShoppingCart.cfm

<cfif NOT StructKeyExists(session,'ShoppingCart')>

  <cfset Session.ShoppingCart = StructNew()>

    <cfset Session.ShoppingCart.FirstName = "">

    <cfset Session.ShoppingCart.LastName = "">

    <cfset Session.ShoppingCart.Address = "">

    <cfset Session.ShoppingCart.Products = ArrayNew(2)>

</cfif>

 

<cfif StructKeyExists(form,'productID')>

  <cfset Session.ShoppingCart.Products[arraylen(Session.ShoppingCart.Products)+1][1] = form.productID>

    <cfset Session.ShoppingCart.Products[arraylen(Session.ShoppingCart.Products)][2] = form.productName>

</cfif>

 

<cfif StructKeyExists(url,'deleteID')>

  <cfset variables.products = ArrayNew(2)>

    <cfloop index="i" from="1" to="#arrayLen(Session.ShoppingCart.Products)#">

    <cfif url.deleteID NEQ i>

  <cfset variables.products[arraylen(variables.products)+1][1] = Session.ShoppingCart.Products[i][1]>

            <cfset variables.products[arraylen(variables.products)][2] = Session.ShoppingCart.Products[i][2]>

        </cfif>

    </cfloop>

    <cfset Session.ShoppingCart.Products = variables.products>

</cfif>

 

 

<cfif StructKeyExists(url,'editID')>

  <cfset Session.ShoppingCart.Products[form.editID][1] = form.productID>

    <cfset Session.ShoppingCart.Products[form.editID][2] = form.productName>

</cfif>

 

 

<cfoutput>

You have #arrayLen(Session.ShoppingCart.Products)# items in your cart.<br /><br />

<cfloop index="i" from="1" to="#arrayLen(Session.ShoppingCart.Products)#">

<table>

<tr>

<td>#Session.ShoppingCart.Products[i][2]#</td>

<td><a href="ShoppingCart.cfm?deleteID=#i#">Delete</a></td>

</tr>

 

</table>

</cfloop>

</cfoutput>

 

<br />

<form action="shopping.cfm" method="POST">

 

  <div class="form-row">

    <label>

      <span>First Name</span>

      <input type="text" size="20" name="FirstName">

    </label>

  </div>

 

    <div class="form-row">

    <label>

      <span>Last Name</span>

      <input type="text" size="20" name="LastName">

    </label>

  </div>

 

    <div class="form-row">

    <label>

      <span>Address</span>

      <input type="text" size="20" name="Address">

    </label>

  </div>

 

  <input type="submit" class="submit" value="Check Out">

</form>

 

Thanks for your help.

Andy


Viewing all articles
Browse latest Browse all 5794

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>