예제 #1
0
파일: Cart.php 프로젝트: jigoshop/Jigoshop2
 /**
  * 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);
 }
예제 #2
0
 /**
  * Returns unique key for product in the cart.
  *
  * @param $item Item Item to get key for.
  *
  * @return string
  */
 public function generateItemKey(Item $item)
 {
     $parts = array($item->getProduct()->getId());
     $parts = $this->wp->applyFilters('jigoshop\\cart\\generate_item_key', $parts, $item);
     return hash('md5', join('_', $parts));
 }
예제 #3
0
 /**
  * Returns HTML for product data.
  *
  * Calls `jigoshop\helper\product\item_data` filter with current data and order item.
  *
  * @param Entity\Order\Item $item Item to display data for.
  *
  * @return string HTML data of the item.
  */
 public static function getItemData(Entity\Order\Item $item)
 {
     $data = '';
     if ($item->getType() == Entity\Product\Variable::TYPE) {
         /** @var Entity\Product\Variable $product */
         $product = $item->getProduct();
         $variation = $product->getVariation($item->getMeta('variation_id')->getValue());
         $data .= self::getVariation($variation, $item);
     }
     return apply_filters('jigoshop\\helper\\product\\item_data', $data, $item);
 }