Exemple #1
0
 /**
  * Adds item to the cart.
  * If item is already present - increases it's quantity.
  *
  * @param Item $item Item to add to cart.
  *
  * @throws NotEnoughStockException When user requests more than we have.
  * @throws Exception On any error.
  */
 public function addItem(Item $item)
 {
     $product = $item->getProduct();
     $quantity = $item->getQuantity();
     if ($product === null || $product->getId() === 0) {
         throw new Exception(__('Product not found', 'jigoshop'));
     }
     if ($quantity <= 0) {
         throw new Exception(__('Quantity has to be positive number', 'jigoshop'));
     }
     if ($this->hasItem($item->getKey())) {
         /** @var Item $itemInCart */
         $itemInCart = $this->getItem($item->getKey());
         if ($product instanceof Product\Purchasable && !$this->checkStock($product, $itemInCart->getQuantity() + $item->getQuantity())) {
             throw new NotEnoughStockException($product->getStock()->getStock());
         }
         $itemInCart->setQuantity($itemInCart->getQuantity() + $item->getQuantity());
         return;
     }
     if ($product instanceof Product\Purchasable && !$this->checkStock($product, $quantity)) {
         throw new NotEnoughStockException($product->getStock()->getStock());
     }
     $isValid = apply_filters('jigoshop\\cart\\validate_new_item', true, $product->getId(), $item->getQuantity());
     if (!$isValid) {
         throw new Exception(__('Could not add to cart.', 'jigoshop'));
     }
     $item = apply_filters('jigoshop\\cart\\new_item', $item);
     parent::addItem($item);
 }