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
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
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
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
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;
}
}
?>