public function addToCart($value, $product) { if ($product instanceof Entity) { $item = new Item(); $item->setName($product->getName()); $item->setPrice($product->getPrice()); $item->setQuantity(1); $item->setProduct($product); return $item; } return $value; }
/** * 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); }
/** * @param $item Item Order item to calculate tax for. * @param $order OrderInterface The order. * * @return array List of tax values per tax class. */ public function getForOrder(Item $item, OrderInterface $order) { return $this->get($item->getCost(), $item->getTaxClasses(), $order->getTaxDefinitions()); }
/** * (PHP 5 >= 5.1.0)<br/> * String representation of object * * @link http://php.net/manual/en/serializable.serialize.php * @return string the string representation of the object or null */ public function serialize() { return serialize(array('item' => $this->item->getId(), 'key' => $this->key, 'value' => $this->value)); }
/** * @param $id int Order ID. * * @return array List of items assigned to the order. */ private function getItems($id) { $wpdb = $this->wp->getWPDB(); $query = $wpdb->prepare("\n\t\t\tSELECT * FROM {$wpdb->prefix}jigoshop_order_item joi\n\t\t\tLEFT JOIN {$wpdb->prefix}jigoshop_order_item_meta joim ON joim.item_id = joi.id\n\t\t\tWHERE joi.order_id = %d\n\t\t\tORDER BY joi.id", array($id)); $results = $wpdb->get_results($query, ARRAY_A); $items = array(); for ($i = 0, $endI = count($results); $i < $endI;) { $id = $results[$i]['id']; $product = $this->productService->find($results[$i]['product_id']); $item = new Entity\Item(); $item->setId($results[$i]['item_id']); $item->setName($results[$i]['title']); $item->setQuantity($results[$i]['quantity']); $item->setPrice($results[$i]['price']); $item->setTax($results[$i]['tax']); while ($i < $endI && $results[$i]['id'] == $id) { // Securing against empty meta's, but still no piece of code does not add the meta. if ($results[$i]['meta_key']) { $meta = new Entity\Item\Meta(); $meta->setKey($results[$i]['meta_key']); $meta->setValue($results[$i]['meta_value']); $item->addMeta($meta); } $i++; } $product = $this->wp->applyFilters('jigoshop\\factory\\order\\find_product', $product, $item); $item->setProduct($product); $item->setKey($this->productService->generateItemKey($item)); $items[] = $item; } return $items; }
public function addToCart($value, $product) { if ($product instanceof Product\Variable) { $item = new Item(); $item->setProduct($product); $variation = $this->factory->getVariation($product, $_POST['variation_id']); foreach ($variation->getAttributes() as $attribute) { /** @var $attribute \Jigoshop\Entity\Product\Variable\Attribute */ if ($attribute->getValue() === '') { $meta = new Item\Meta(); $metaValue = isset($_POST['attributes']) ? $_POST['attributes'][$attribute->getAttribute()->getId()] : 'any'; $meta->setKey($attribute->getAttribute()->getSlug()); $meta->setValue($metaValue); $item->addMeta($meta); $attribute->setValue($metaValue); } } $item->setName($variation->getTitle()); $item->setPrice($variation->getProduct()->getPrice()); $item->setQuantity($_POST['quantity']); $item->setTaxClasses($variation->getProduct()->getTaxClasses()); $meta = new Item\Meta(); $meta->setKey('variation_id'); $meta->setValue($variation->getId()); $item->addMeta($meta); return $item; } return $value; }
/** * 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)); }
/** * @param $value * @param $product * * @return null */ public function addToCart($value, $product) { if ($product instanceof Entity) { $item = new Item(); $item->setName($product->getName()); $item->setPrice($product->getPrice()); $item->setQuantity(1); $item->setProduct($product); $meta = new Item\Meta('file', $product->getUrl()); $item->addMeta($meta); $meta = new Item\Meta('downloads', $product->getLimit()); $item->addMeta($meta); return $item; } return $value; }
/** * 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); }