Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Shopping Cart Component
- *
- */
- component accessors="true" {
- /**
- * the shopping cart array of items. we simply hold an item that contains an id, qty
- */
- property name="cart" type="array";
- /**
- * initialize the shopping cart instance
- *
- * @return ShoppingCart this: the instance of the class created
- */
- public ShoppingCart function init(){
- setCart([]);
- return this;
- }
- /**
- * When an item is added to the cart I am called. If the item already exists
- * in the cart we will update the current qty
- *
- * @param numeric id: the product id
- * @param numberic qty: the qty of product to add/update or to the cart array
- */
- public void function add(id,qty){
- var item = {};
- var position = getCartPosition(arguments.id);
- if( isNumeric(qty) && qty > 0 ){
- if( position ){
- update(position,qty);
- } else {
- item.id = arguments.id;
- item.qty = arguments.qty;
- arrayAppend(variables.cart,item);
- }
- } else {
- throw(type="ShoppingCart",message="QTY Must be a valid integer.");
- }
- }
- /**
- * If the item is already in our cart when add() is called we internally
- * call this method to update the carts qty
- *
- * @param Numeric position: the position in the array where the product exists
- * @param Numeric qty: The qty to add to the current qty in the array
- */
- private void function update(position,qty){
- variables.cart[position].qty += arguments.qty;
- }
- /**
- * I will remove a product from the cart array by id. If the product is not
- * found in the cart nothing happens and false is returned to the caller
- *
- * @param Numeric id: the product id
- * @return Boolean removed: if a product was removed from the cart array
- */
- public Boolean function remove(id){
- var removed = false;
- var position = getCartPosition(arguments.id);
- // make sure that we are dealing with a valid item in the array
- if( position ){
- arrayDeleteAt(variables.cart,position);
- removed = true;
- }
- return removed;
- }
- /**
- * I will remove all of the items from the cart by clearing the array
- *
- */
- public void function clear(){
- setCart([]);
- }
- /**
- * By default I will return the number of distinct items in the cart (products)
- * If you want to know how many total items are in the cart, pass false to this method
- *
- * @param Boolean distinct: should we return a distinct product list or total number of items
- * @return Numeric count: number of distinct products or total items in the cart
- */
- public Numeric function count(Boolean distinct=true){
- var count = 0;
- if( distinct ){
- count = arrayLen(getCart());
- } else {
- for(var x=1; x<=arrayLen(getCart()); x++){
- count += getCart()[x].qty;
- }
- }
- return count;
- }
- /**
- * I will search the array for a product to see if it already exists
- * If it does exist I will return the position in the array, else 0
- *
- * @param Numeric id: the product id
- * @return Numeric position: the position in the cart array of the id
- */
- private Numeric function getCartPosition(id){
- var position = 0;
- if( arrayLen(getCart()) ) {
- for(var x=1; x<=arrayLen(getCart()); x++){
- if( getCart()[x].id == arguments.id) {
- position = x;
- break;
- }
- }
- }
- return position;
- }
- }
Add Comment
Please, Sign In to add comment