/** * Finds item specified by ID. * * @param $id int The ID. * @return Product */ public function find($id) { if (!isset($this->objects[$id])) { $this->objects[$id] = $this->service->find($id); } return $this->objects[$id]; }
public function action() { if (isset($_POST['action']) && $_POST['action'] == 'add-to-cart') { /** @var Entity $product */ $product = $this->productService->find($_POST['item']); try { $item = $this->wp->applyFilters('jigoshop\\cart\\add', null, $product); if ($item === null) { throw new Exception(__('Unable to add product to the cart.', 'jigoshop')); } $cart = $this->cartService->get($this->cartService->getCartIdForCurrentUser()); $cart->addItem($item); $this->cartService->save($cart); $url = false; $button = ''; switch ($this->options->get('shopping.redirect_add_to_cart')) { case 'cart': $url = $this->wp->getPermalink($this->options->getPageId(Pages::CART)); break; case 'checkout': $url = $this->wp->getPermalink($this->options->getPageId(Pages::CHECKOUT)); break; case 'product': default: $url = $this->wp->getPermalink($product->getId()); case 'same_page': case 'product_list': $button = sprintf('<a href="%s" class="btn btn-warning pull-right">%s</a>', $this->wp->getPermalink($this->options->getPageId(Pages::CART)), __('View cart', 'jigoshop')); } $this->messages->addNotice(sprintf(__('%s successfully added to your cart. %s', 'jigoshop'), $product->getName(), $button)); if ($url !== false) { $this->messages->preserveMessages(); $this->wp->wpRedirect($url); exit; } } catch (NotEnoughStockException $e) { if ($e->getStock() == 0) { $message = sprintf(__('Sorry, we do not have "%s" in stock.', 'jigoshop'), $product->getName()); } else { if ($this->options->get('products.show_stock')) { $message = sprintf(__('Sorry, we do not have enough "%s" in stock to fulfill your order. We only have %d available at this time. Please edit your cart and try again. We apologize for any inconvenience caused.', 'jigoshop'), $product->getName(), $e->getStock()); } else { $message = sprintf(__('Sorry, we do not have enough "%s" in stock to fulfill your order. Please edit your cart and try again. We apologize for any inconvenience caused.', 'jigoshop'), $product->getName()); } } $this->messages->addError($message); } catch (Exception $e) { $this->messages->addError(sprintf(__('A problem ocurred when adding to cart: %s', 'jigoshop'), $e->getMessage()), false); } } }
/** * @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 ajaxRemoveVariation() { try { if (!isset($_POST['product_id']) || empty($_POST['product_id'])) { throw new Exception(__('Product was not specified.', 'jigoshop')); } if (!is_numeric($_POST['product_id'])) { throw new Exception(__('Invalid product ID.', 'jigoshop')); } if (!isset($_POST['variation_id']) || empty($_POST['variation_id'])) { throw new Exception(__('Variation was not specified.', 'jigoshop')); } if (!is_numeric($_POST['variation_id'])) { throw new Exception(__('Invalid variation ID.', 'jigoshop')); } $product = $this->productService->find((int) $_POST['product_id']); if (!$product->getId()) { throw new Exception(__('Product does not exists.', 'jigoshop')); } if (!$product instanceof Product\Variable) { throw new Exception(__('Product is not variable - unable to add variation.', 'jigoshop')); } $variation = $product->removeVariation((int) $_POST['variation_id']); $this->service->removeVariation($variation); $this->productService->save($product); echo json_encode(array('success' => true)); } catch (Exception $e) { echo json_encode(array('success' => false, 'error' => $e->getMessage())); } exit; }
public function ajaxFindProduct() { try { $products = array(); if (isset($_POST['query'])) { $query = trim(htmlspecialchars(strip_tags($_POST['query']))); if (!empty($query)) { $products = $this->productService->findLike($query); } } else { if (isset($_POST['value'])) { $query = explode(',', trim(htmlspecialchars(strip_tags($_POST['value'])))); foreach ($query as $id) { $products[] = $this->productService->find($id); } } else { throw new Exception(__('Neither query nor value is provided to find products.', 'jigoshop')); } } $result = array('success' => true, 'results' => $this->prepareResults($products)); } catch (Exception $e) { $result = array('success' => false, 'error' => $e->getMessage()); } echo json_encode($result); exit; }
/** * @param $product VariableProduct Product to fetch variations for. * * @return array List of variations. */ public function getVariations($product) { $wpdb = $this->wp->getWPDB(); $query = $wpdb->prepare("\n\t\t\tSELECT pv.ID, pva.* FROM {$wpdb->posts} pv\n\t\t\t\tLEFT JOIN {$wpdb->prefix}jigoshop_product_variation_attribute pva ON pv.ID = pva.variation_id\n\t\t\t\tWHERE pv.post_parent = %d AND pv.post_type = %s\n\t\t", array($product->getId(), \Jigoshop\Core\Types\Product\Variable::TYPE)); $results = $wpdb->get_results($query, ARRAY_A); $variations = array(); $results = array_filter($results, function ($item) { return $item['attribute_id'] !== null; }); for ($i = 0, $endI = count($results); $i < $endI;) { $variation = new VariableProduct\Variation(); $variation->setId((int) $results[$i]['ID']); $variation->setParent($product); /** @var Product $variableProduct */ $variableProduct = $this->productService->find($results[$i]['ID']); $variation->setProduct($variableProduct); // TODO: Maybe some kind of fetching together? while ($i < $endI && $results[$i]['ID'] == $variation->getId()) { $attribute = new VariableProduct\Attribute(VariableProduct\Attribute::VARIATION_ATTRIBUTE_EXISTS); $attribute->setVariation($variation); $attribute->setAttribute($product->getAttribute($results[$i]['attribute_id'])); $attribute->setValue($results[$i]['value']); if ($attribute->getAttribute() !== null) { $variation->addAttribute($attribute); } $i++; } $variations[$variation->getId()] = $variation; } return $variations; }
public function displayColumn($column) { $post = $this->wp->getGlobalPost(); if ($post === null) { return; } /** @var Product | Product\Variable $product */ $product = $this->productService->find($post->ID); switch ($column) { case 'thumbnail': echo ProductHelper::getFeaturedImage($product, Options::IMAGE_THUMBNAIL); break; case 'price': echo ProductHelper::getPriceHtml($product); break; case 'featured': echo ProductHelper::isFeatured($product); break; case 'type': echo $this->type->getType($product->getType())->getName(); break; case 'sku': echo $this->getVariableAdditionalInfo($product, 'sku'); break; case 'stock': echo $this->getVariableAdditionalInfo($product, 'stock'); break; case 'creation': $timestamp = strtotime($post->post_date); echo Formatter::date($timestamp); if ($product->isVisible()) { echo '<br /><strong>' . __('Visible in', 'jigoshop') . '</strong>: '; switch ($product->getVisibility()) { case ProductEntity::VISIBILITY_SEARCH: echo __('Search only', 'jigoshop'); break; case ProductEntity::VISIBILITY_CATALOG: echo __('Catalog only', 'jigoshop'); break; case ProductEntity::VISIBILITY_PUBLIC: echo __('Catalog and search', 'jigoshop'); break; } } break; } }
/** * @param $variation Product\Variable\Variation * @param $product Product\Variable * * @return Product */ private function _createVariableProduct($variation, $product) { $variableId = $this->createVariablePost($variation); /** @var Product|Product\Purchasable|Product\Saleable $variableProduct */ $variableProduct = $this->productService->find($variableId); $variableProduct->setVisibility(Product::VISIBILITY_NONE); $variableProduct->setTaxable($product->isTaxable()); $variableProduct->setTaxClasses($product->getTaxClasses()); $variableProduct->getStock()->setManage(true); if ($variableProduct instanceof Product\Saleable) { $variableProduct->getSales()->unserialize($product->getSales()->serialize()); } return $variableProduct; }
protected function _fetchItemData($args) { $defaults = array('id' => 0, 'variation_id' => 0, 'variation' => array(), 'cost_inc_tax' => 0, 'name' => '', 'qty' => 1, 'cost' => 0, 'taxrate' => 0); if ($args['variation_id'] > 0) { $post = $this->wp->getPost($args['variation_id']); if ($post) { /** @var Product\Variable $product */ $product = $this->productService->find($post->post_parent); if ($product->getId() && $product instanceof Product\Variable) { $args['name'] = $product->getVariation($post->ID)->getTitle(); } } } return $this->_fetchData($defaults, $args); }