/** * Displays the widget in the sidebar. * * @param array $args Sidebar arguments. * @param array $instance The instance. * * @return bool|void */ public function widget($args, $instance) { // Get the best selling products from the transient $cache = get_transient(Core::WIDGET_CACHE); // If cached get from the cache if (isset($cache[$args['widget_id']])) { echo $cache[$args['widget_id']]; return; } // Start buffering ob_start(); // Set the widget title $title = apply_filters('widget_title', $instance['title'] ? $instance['title'] : __('Best Sellers', 'jigoshop'), $instance, $this->id_base); // Set number of products to fetch if (!($number = absint($instance['number']))) { $number = 5; } // Set up query $query_args = array('posts_per_page' => $number, 'post_type' => Types::PRODUCT, 'post_status' => 'publish', 'meta_key' => 'stock_sold', 'orderby' => 'meta_value_num+0', 'order' => 'desc', 'nopaging' => false, 'meta_query' => array(array('key' => 'visibility', 'value' => array(Product::VISIBILITY_CATALOG, Product::VISIBILITY_PUBLIC), 'compare' => 'IN'))); // Run the query $q = new \WP_Query($query_args); $products = self::$productService->findByQuery($q); if (!empty($products)) { Render::output('widget/best_sellers/widget', array_merge($args, array('title' => $title, 'products' => $products))); } // Flush output buffer and save to transient cache $cache[$args['widget_id']] = ob_get_flush(); set_transient(Core::WIDGET_CACHE, $cache, 3600 * 3); // 3 hours ahead }
/** * Displays the widget in the sidebar. * * @param array $args Sidebar arguments. * @param array $instance The instance. */ public function widget($args, $instance) { // Check if session contains recently viewed products if (!isset($_SESSION[self::SESSION_KEY]) || empty($_SESSION[self::SESSION_KEY])) { return; } // Start buffering the output ob_start(); // Set the widget title $title = apply_filters('widget_title', $instance['title'] ? $instance['title'] : __('Recently Viewed Products', 'jigoshop'), $instance, $this->id_base); // Set number of products to fetch if (!($number = absint($instance['number']))) { $number = 5; } // Set up query $query_args = array('posts_per_page' => $number, 'post_type' => Core\Types::PRODUCT, 'post_status' => 'publish', 'nopaging' => true, 'post__in' => $_SESSION[self::SESSION_KEY], 'meta_query' => array(array('key' => 'visibility', 'value' => array(Product::VISIBILITY_CATALOG, Product::VISIBILITY_PUBLIC), 'compare' => 'IN'))); // Run the query $q = new \WP_Query($query_args); $products = self::$productService->findByQuery($q); $ordered = array(); foreach ($_SESSION[self::SESSION_KEY] as $key) { if (isset($products[$key])) { $ordered[$key] = $products[$key]; } } $products = $ordered; if (!empty($products)) { Render::output('widget/recently_viewed_products/widget', array_merge($args, array('title' => $title, 'products' => $products))); } }
public function render() { $query = $this->wp->getQuery(); $products = $this->productService->findByQuery($query); $content = do_shortcode($this->getContent()); return Render::get('shop', array('content' => $content, 'products' => $products, 'product_count' => $query->max_num_pages, 'messages' => $this->messages, 'title' => $this->getTitle())); }
/** * Get related products based on the same parent product category. * @param \Jigoshop\Entity\Product $product * * @return array */ protected function getRelated($product) { if (!$this->options->get('products.related')) { return array(); } $count = $this->wp->applyFilters('jigoshop/frontend/page/product/render/related_products_count', 3); return $this->productService->findByQuery(ProductHelper::getRelated($product, $count)); }
/** * Finds items specified using WordPress query. * * @param $query \WP_Query WordPress query. * @return array Collection of found items. */ public function findByQuery($query) { $hash = hash('md5', serialize($query->query_vars)); if (!isset($this->queries[$hash])) { $this->queries[$hash] = $this->service->findByQuery($query); } return $this->queries[$hash]; }
/** * Displays the widget in the sidebar. * * @param array $args Sidebar arguments. * @param array $instance The instance. * * @return bool|void */ public function widget($args, $instance) { ob_start(); // Set the widget title $title = apply_filters('widget_title', $instance['title'] ? $instance['title'] : __('Featured Products', 'jigoshop'), $instance, $this->id_base); // Set number of products to fetch if (!($number = absint($instance['number']))) { $number = 5; } // Set up query $query_args = array('posts_per_page' => $number, 'post_type' => Types::PRODUCT, 'post_status' => 'publish', 'meta_key' => 'featured', 'meta_value' => '1', 'meta_query' => array(array('key' => 'visibility', 'value' => array(Product::VISIBILITY_CATALOG, Product::VISIBILITY_PUBLIC), 'compare' => 'IN'))); // Run the query $q = new \WP_Query($query_args); $products = self::$productService->findByQuery($q); if (!empty($products)) { Render::output('widget/featured_products/widget', array_merge($args, array('title' => $title, 'products' => $products))); } }