/** * Cart constructor * * @param void * @return void */ public function __construct() { parent::__construct(); $this->cart = new stdClass(); /* Load current user cart */ /* Check if there is a session or cookie cart */ // Get cart from session $cart = $this->liftSessionCart(); // If no session cart, try to locate a cookie cart (only for not logged in users) if (!$cart && User::isGuest()) { $cart = $this->liftCookie(); } if ($cart) { // If cart found and user is logged in, verify if the cart is linked to the user cart in the DB if (!User::isGuest()) { if (empty($this->cart->linked) || !$this->cart->linked) { // link carts if not linked (this should only happen when user logs in with a cart created while not logged in) // if linking fails create a new cart if (!$this->linkCarts()) { $this->createCart(); } } } else { $this->cart->linked = 0; } } elseif (!User::isGuest()) { // Try to get the saved cart in the DB if (!$this->liftUserCart(User::get('id'))) { // If no session, no cookie, no DB cart -- create a brand new cart $this->createCart(); } } else { $this->createCart(); } }
/** * Cart constructor * * @param int Cart ID to locate the specific cart * @return void */ public function __construct($crtId) { // Cart ID is always an integer if (empty($crtId) || !is_numeric($crtId) || $crtId < 1) { throw new Exception('Bad cart initialization'); } parent::__construct(); $this->crtId = $crtId; if (!$this->exists()) { throw new Exception(COM_CART_NO_CART_FOUND); } }