Guest User

Untitled

a guest
Feb 21st, 2018
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Shopping Cart Component
  3.  *
  4.  */
  5. component accessors="true" {
  6.    
  7.     /**
  8.      * the shopping cart array of items. we simply hold an item that contains an id, qty
  9.      */
  10.     property name="cart" type="array";
  11.  
  12.     /**
  13.      * initialize the shopping cart instance
  14.      *
  15.      * @return ShoppingCart this: the instance of the class created
  16.      */
  17.     public ShoppingCart function init(){
  18.         setCart([]);
  19.         return this;
  20.     }
  21.  
  22.     /**
  23.      * When an item is added to the cart I am called. If the item already exists
  24.      * in the cart we will update the current qty
  25.      *
  26.      * @param numeric id: the product id
  27.      * @param numberic qty: the qty of product to add/update or to the cart array
  28.      */
  29.     public void function add(id,qty){
  30.         var item = {};
  31.         var position = getCartPosition(arguments.id);
  32.  
  33.         if( isNumeric(qty)  && qty > 0 ){
  34.             if( position ){
  35.                 update(position,qty);      
  36.             } else {
  37.                 item.id = arguments.id;
  38.                 item.qty = arguments.qty;
  39.                 arrayAppend(variables.cart,item);
  40.             }
  41.         } else {
  42.             throw(type="ShoppingCart",message="QTY Must be a valid integer.");
  43.         }
  44.  
  45.     }
  46.  
  47.     /**
  48.      * If the item is already in our cart when add() is called we internally
  49.      * call this method to update the carts qty
  50.      *
  51.      * @param Numeric position: the position in the array where the product exists
  52.      * @param Numeric qty: The qty to add to the current qty in the array
  53.      */
  54.     private void function update(position,qty){
  55.         variables.cart[position].qty += arguments.qty;
  56.     }
  57.  
  58.     /**
  59.      * I will remove a product from the cart array by id. If the product is not
  60.      * found in the cart nothing happens and false is returned to the caller
  61.      *
  62.      * @param Numeric id: the product id
  63.      * @return Boolean removed: if a product was removed from the cart array
  64.      */
  65.     public Boolean function remove(id){
  66.         var removed = false;
  67.         var position = getCartPosition(arguments.id);
  68.  
  69.         // make sure that we are dealing with a valid item in the array
  70.         if( position ){
  71.             arrayDeleteAt(variables.cart,position);
  72.             removed = true;
  73.         }
  74.  
  75.         return removed;
  76.     }
  77.  
  78.     /**
  79.      * I will remove all of the items from the cart by clearing the array
  80.      *
  81.      */
  82.     public void function clear(){
  83.         setCart([]);
  84.     }
  85.  
  86.     /**
  87.      * By default I will return the number of distinct items in the cart (products)
  88.      * If you want to know how many total items are in the cart, pass false to this method
  89.      *
  90.      * @param Boolean distinct: should we return a distinct product list or total number of items
  91.      * @return Numeric count: number of distinct products or total items in the cart
  92.      */
  93.     public Numeric function count(Boolean distinct=true){
  94.         var count = 0;
  95.  
  96.         if( distinct ){
  97.             count = arrayLen(getCart());
  98.         } else {
  99.             for(var x=1; x<=arrayLen(getCart()); x++){
  100.                 count += getCart()[x].qty;
  101.             }
  102.         }
  103.  
  104.         return count;
  105.     }
  106.  
  107.  
  108.     /**
  109.      * I will search the array for a product to see if it already exists
  110.      * If it does exist I will return the position in the array, else 0
  111.      *
  112.      * @param Numeric id: the product id
  113.      * @return Numeric position: the position in the cart array of the id
  114.      */
  115.     private Numeric function getCartPosition(id){
  116.         var position = 0;
  117.  
  118.         if( arrayLen(getCart()) ) {
  119.             for(var x=1; x<=arrayLen(getCart()); x++){
  120.                 if( getCart()[x].id == arguments.id) {
  121.                     position = x;
  122.                     break;
  123.                 }
  124.             }
  125.         }
  126.        
  127.         return position;       
  128.     }
  129.  
  130. }
Add Comment
Please, Sign In to add comment