SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question I can't add product to shopping cart, please help me!

    I'm trying add product to my shopping cart, but with new product, it won't be created and the quantity of old product is increment, please see my code:

    test.php
    PHP Code:
    <?php
        
    include_once 'cart/Product.php';
        include_once 
    'cart/ShoppingCartItem.php';
        include_once 
    'cart/ShoppingCart.php';
        
        
    $ford = new Product(1"ford"25000"08/03/2013");
        
    $rollroy = new Product(2"roll roy"26000"8/3/2013");
        
        
    $shopping_cart->addItem($ford);
        
    $shopping_cart->addItem($ford);
        
    $shopping_cart->addItem($ford);
        
    $shopping_cart->addItem($rollroy);
        
        
    $allProducts $shopping_cart->getItems();
        
        for (
    $i 0$i count($allProducts); $i++) {
            echo 
    "<br />";
            echo 
    $allProducts[$i]->getProduct()->id."<br />";
            echo 
    $allProducts[$i]->getProduct()->name."<br />";
            echo 
    $allProducts[$i]->getQuantity();
        }
        
    ?>
    Result is 2 - ford - 4

    ShoppingCart.php
    PHP Code:
    <?php
        
    class ShoppingCart {
            
            private 
    $items = array();
            private 
    $numberOfItem;
            private 
    $total;
            
            public function 
    ShoppingCart() {
                
    $this->numberOfItem 0;
                
    $this->total 0;
            }
            
            
    /**
             * Adds ShoppingCartItem to the ShoppingCart
             * If item existed, the quantity is increment and not
             * create new ShoppingCartItem
             * 
             * @param Product $product
             * @see ShoppingCartItem
             */
            
    public function addItem(Product $product) {
                
                
    $newItem true;
                
                for (
    $i 0$i<count($this->items); $i++) {
                    if (
    $this->items[$i]->getProduct()->getId() == $product->getId()) {
                        
    $newItem FALSE;
                        
    $this->items[$i]->incrementQuantity();
                    }
                }
                
                if (
    $newItem) {
                    
    $scItem = new ShoppingCartItem($product);
                    
    $this->items[] = $scItem;
                }
            }
            
        }
    ?>
    ShoppingCartItem.php
    PHP Code:
    <?php
        
    class ShoppingCartItem {
            
            private 
    $product;
            private 
    $quantity;

            public function 
    ShoppingCartItem(Product $product) {
                
    $this->product $product;
                
    $this->quantity 1;
            }
            
            public function 
    getProduct() {
                return 
    $this->product;
            }
            
            public function 
    getQuantity() {
                return 
    $this->quantity;
            }
            
            public function 
    setQuantity($quantity) {
                
    $this->quantity $quantity;
            }
            
            public function 
    incrementQuantity() {
                
    $this->quantity++;
            }
            
            public function 
    decrementQuantity() {
                
    $this->quantity--;
            }
            
            public function 
    getTotal() {
                
    $amount 0;
                
    $amount = ($this->getQuantity() * (double)$this->product->getPrice());
                return 
    $amount;
            }

        }
    ?>
    and Product.php
    PHP Code:
    <?php
        
    class Product {
            private 
    $id;
            private 
    $name;
            private 
    $price;
            private 
    $lastUpdate;
            private 
    $category;
            
            public function 
    Product($id$name$price$lastUpdate) {
                
    $this->id$id;
                
    $this->name $name;
                
    $this->price $price;
                
    $this->lastUpdate $lastUpdate;
            }
            
            public function 
    __get($property) {
                
    $method "get{$property}";
                if (
    method_exists($this$method)) {
                    return 
    $this->$method();
                };
            }
            
            public function 
    __set($property$value) {
                
    $method "set{$property}";
                if (
    method_exists($this$method)) {
                    return 
    $this->$method($value);
                }
            }

            public function 
    getId() {
                return 
    $this->id;
            }
            
            public function 
    setId($id) {
                
    $this->id $id;
            }
            
            public function 
    getName() {
                return 
    $this->name;
            }
            
            public function 
    setName($name) {
                
    $this->name $name;
            }
            
            public function 
    getPrice() {
                return 
    $this->price;
            }
            
            public function 
    setPrice($price) {
                
    $this->price $price;
            }
            
            public function 
    getLastUpdate() {
                return 
    $this->lastUpdate;
            }
            
            public function 
    setLastUpdate($lastUpdate) {
                
    $this->lastUpdate $lastUpdate;
            }
            
            public function 
    getCategory() {
                return 
    $this->category;
            }
            
            public function 
    setCategory($category) {
                
    $this->category $category;
            }
        }
    ?>

  2. #2
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I found errors, thank for watching!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •