Example #1
0
 /**
  * @param      $product Product or product_srl
  * @param int  $quantity
  * @param bool $relativeQuantity
  *
  * @return mixed
  * @throws Exception
  */
 public function addProduct($product, $quantity = 1, $relativeQuantity = true)
 {
     if (!$this->isPersisted()) {
         throw new ShopException('Cart is not persisted');
     }
     if ($product instanceof SimpleProduct) {
         if (!($product_srl = $product->product_srl)) {
             throw new ShopException('Product is not persisted');
         }
     } elseif (is_numeric($product)) {
         $product = new SimpleProduct($product);
     } else {
         throw new ShopException('Wrong $product input for addProduct to cart');
     }
     if (!$this->productStillAvailable($product)) {
         throw new ShopException('Product is no longer available, cannot add it to cart.');
     }
     if ($cp = $this->getCartProduct($product)) {
         $cartProductQuantity = $relativeQuantity ? $cp->quantity + $quantity : $quantity;
         $return = $this->setProductQuantity($product->product_srl, $cartProductQuantity, array('title' => $product->title));
     } else {
         $cartProductQuantity = $quantity;
         $return = $this->repo->insertCartProduct($this->cart_srl, $product->product_srl, $cartProductQuantity, array('title' => $product->title, 'price' => $product->getPrice()));
     }
     $this->setExtra('price', $this->getPrice(true));
     $this->items = $this->count(true, true);
     $this->save();
     return $return;
 }