public static function addProduct($id) { if (!isset($_SESSION["cart"])) { $_SESSION["cart"] = []; } $product = ProductManager::getProduct($id); $cartProduct = new CartProduct($product, 1); if (array_key_exists($cartProduct->getId(), $_SESSION["cart"])) { $quantity = $_SESSION["cart"][$cartProduct->getId()]->getQuantity(); $_SESSION["cart"][$cartProduct->getId()]->setQuantity($quantity + 1); } else { $_SESSION["cart"][$cartProduct->getId()] = $cartProduct; } }
public static function getAll() { $aAll = array(); if (array_key_exists('cart', $_SESSION)) { foreach ($_SESSION['cart'] as $iProductId => $iQuantity) { $oProduct = ProductManager::get($iProductId); if ($oProduct) { // product should exists ! if (false === $oProduct) { continue; } // convert to CartProduct $oProduct = CartProduct::create($oProduct); $oProduct->setQuantity($iQuantity); $aAll[] = $oProduct; } } } return $aAll; }
private function addtocartAction() { $oCartProduct = new CartProduct(); $oCartProduct->setId(intval($_POST['product'])); $oCartProduct->setQuantity(intval($_POST['quantity'])); CartManager::add($oCartProduct); }