# Shopping cart help

**URL:** <https://forum.kirupa.com/t/shopping-cart-help/323367>\
**Category:** flash\
**Created:** [June 27, 2011, 9:32pm UTC](https://forum.kirupa.com/t/shopping-cart-help/323367 "2011-06-27T21:32:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![dutch106](https://avatars.discourse-cdn.com/v4/letter/d/74df32/32.png) [@dutch106](https://forum.kirupa.com/u/dutch106)\
**Post date:** [June 27, 2011, 9:32pm UTC](https://forum.kirupa.com/t/shopping-cart-help/323367/1 "2011-06-27T21:32:31Z")

</div>

This shopping cart is close to what I need , however it is not completely doing what I would like it to do and am looking for some help.

When someone presses the “add to shopping list” button, I would like the product information to load, but not be visible until the user is done adding all the products they want into the shopping list. Then I want them to be able to click on a “shopping list” tab at the top of the page and it will take them to the frame that holds the shopping list products they added.

Any ideas on how I can accomplish this with the code I have would be appreciated.

Here is the code I am using on Frame 1 of the timeline:

import Product;  
import Shopping;

var shop\_cart = new Shopping();  
displayCart();

function displayCart(){  
//Create main movie clip to hold shopping cart output  
empty\_mc = attachMovie(“empty\_mc”,“empty\_mc2”,150);  
//Create movie clip to hold shopping cart output headings  
temp\_mc = empty\_mc.attachMovie(“display\_mc”,“headings\_mc”,10 01);  
temp\_mc.total.text = “Total”;  
temp\_mc.desc.text = “Description”;  
temp\_mc.quantity.text = “Quantity”;  
temp\_mc.price.text = “Price”;  
temp\_mc.update\_btn.\_visible = false;  
delete temp\_mc;  
//Loop through items in shopping cart  
for(i=0;i\<shop\_cart.size();i++){  
var newProd:Product = shop\_cart.getElement(i);  
dataGrid.addItem(newProd)

//Attach display for each item in shopping cart  
temp\_mc = empty\_mc.attachMovie(“display\_mc”,“display\_mc”+i,i +100);  
//Shift display downward 16 pixels  
temp\_mc.\_y = (i + 1) \* 20;  
//Place values from product in shopping cart in the text fields  
temp\_mc.quantity.text=newProd.getQuantity();  
temp\_mc.desc.text=newProd.getDesc();  
temp\_mc.price.text=newProd.getPrice();  
temp\_mc.total.text=newProd.getTotal();  
//Position holder of product in shopping cart  
temp\_mc.pos = i;  
//button action to update quantities  
temp\_mc.update\_btn.onPress = function() {  
var tempProduct:Product = shop\_cart.getElement(this.\_parent.pos);  
if (this.\_parent.quantity.text == “0”){  
shop\_cart.removeElementAt(this.\_parent.pos);  
}  
else {  
tempProduct.setQuantity(this.\_parent.quantity.text );  
}  
displayCart();  
}  
delete newProd;  
}  
//Create movie clip to hold shopping cart Grand Total output  
temp\_mc = empty\_mc.attachMovie(“display\_mc”,“total\_mc”,1000) ;  
temp\_mc.\_y = (shop\_cart.size() +1) \* 20;  
temp\_mc.total.text = shop\_cart.getTotal();  
temp\_mc.desc.text = “Grand Total”;  
temp\_mc.update\_btn.\_visible = false;

## delete temp\_mc; this.grandTotal.text = shop\_cart.getTotal(); }

Here is the code on my “add to shopping list” button:

## on (press){ import Product; var my1Product = new Product(); my1Product.setQuantity(1); my1Product.setPrice (2.99); my1Product.setDesc(“Water”); shop\_cart.addElement(my1Product); delete my1Product; displayCart(); }

Here is the code for my product.as :

class Product {

//Instance Variables  
private var price:Number;  
private var desc:String;  
private var quantity:Number;

public function Product() {

}

public function getPrice():Number {  
return price;  
}

public function setPrice(myVar:Number):Void {  
price = myVar;  
}

public function getQuantity():Number {  
return quantity;  
}

public function setQuantity(quantity:Number):Void {  
this.quantity = quantity;  
}

public function getDesc():String {  
return desc;  
}

public function setDesc(myVar:String):Void {  
desc = myVar;  
}

## public function getTotal():Number { return price\*quantity; } }

Here is the Shopping.as :

import Product;

class Shopping {

private var cart:Array;  
private var elementCount:Number;

public function Shopping(){  
cart = new Array(20);  
elementCount = 0;  
}

public function addElement(object){  
cart[elementCount++] = object;  
}

public function getTotal():Number{  
var total = 0;  
var i;  
for(i=0;i\<elementCount;i++){  
var tempProduct:Product = cart\*;  
total += tempProduct.getTotal();  
}  
return total;  
}

public function size():Number{  
return elementCount;  
}

public function getElement(index){  
return cart[index];  
}

public function removeElementAt(index) {  
if (index \<= elementCount && index \> -1){  
delete cart[index];  
for (var i = index+1; i\<elementCount; i++) {  
cart[i-1] = cart\*;  
}  
elementCount–;  
}  
}

}
