/** * Widget * Display the widget in the sidebar * Save output to the cache if empty * * @param array sidebar arguments * @param array instance */ public function widget($args, $instance) { // Hide widget if page is the cart or checkout if (is_cart() || is_checkout()) { return false; } extract($args); // Set the widget title $title = apply_filters('widget_title', $instance['title'] ? $instance['title'] : __('Cart', 'fflcommerce'), $instance, $this->id_base); // Print the widget wrapper & title echo $before_widget; if ($title) { echo $before_title . $title . $after_title; } // Get the contents of the cart $cart_contents = fflcommerce_cart::$cart_contents; // If there are items in the cart print out a list of products if (!empty($cart_contents)) { // Open the list echo '<ul class="cart_list">'; foreach ($cart_contents as $key => $value) { // Get product instance $_product = $value['data']; if ($_product->exists() && $value['quantity'] > 0) { echo '<li>'; // Print the product image & title with a link to the permalink echo '<a href="' . esc_attr(get_permalink($_product->id)) . '" title="' . esc_attr($_product->get_title()) . '">'; // Print the product thumbnail image if exists else display placeholder echo has_post_thumbnail($_product->id) ? get_the_post_thumbnail($_product->id, 'shop_tiny') : fflcommerce_get_image_placeholder('shop_tiny'); // Print the product title echo '<span class="js_widget_product_title">' . $_product->get_title() . '</span>'; echo '</a>'; // Displays variations and cart item meta echo fflcommerce_cart::get_item_data($value); // Print the quantity & price per product echo '<span class="js_widget_product_price">' . $value['quantity'] . ' × ' . $_product->get_price_html() . '</span>'; echo '</li>'; } } echo '</ul>'; // Close the list // Print the cart total echo '<p class="total"><strong>'; echo __('Subtotal', 'fflcommerce'); echo ':</strong> ' . fflcommerce_price($this->total_cart_items()); echo '</p>'; do_action('fflcommerce_widget_cart_before_buttons'); // Print view cart & checkout buttons $view_cart_button_label = isset($instance['view_cart_button']) ? $instance['view_cart_button'] : __('View Cart →', 'fflcommerce'); $checkout_button_label = isset($instance['checkout_button']) ? $instance['checkout_button'] : __('Checkout →', 'fflcommerce'); echo '<p class="buttons">'; echo '<a href="' . esc_attr(fflcommerce_cart::get_cart_url()) . '" class="button">' . __($view_cart_button_label, 'fflcommerce') . '</a>'; echo '<a href="' . esc_attr(fflcommerce_cart::get_checkout_url()) . '" class="button checkout">' . __($checkout_button_label, 'fflcommerce') . '</a>'; echo '</p>'; } else { echo '<span class="empty">' . __('No products in the cart.', 'fflcommerce') . '</span>'; } // Print closing widget wrapper echo $after_widget; }
/** * Widget * Display the widget in the sidebar * Save output to the cache if empty * * @param array sidebar arguments * @param array instance */ public function widget($args, $instance) { // Get the best selling products from the transient $cache = get_transient('fflcommerce_widget_cache'); // If cached get from the cache if (isset($cache[$args['widget_id']])) { echo $cache[$args['widget_id']]; return false; } // Start buffering ob_start(); extract($args); // Set the widget title $title = apply_filters('widget_title', $instance['title'] ? $instance['title'] : __('Best Sellers', 'fflcommerce'), $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' => 'product', 'post_status' => 'publish', 'meta_key' => 'quantity_sold', 'orderby' => 'meta_value_num+0', 'order' => 'desc', 'nopaging' => false, 'meta_query' => array(array('key' => 'visibility', 'value' => array('catalog', 'visible'), 'compare' => 'IN'))); // Run the query $q = new WP_Query($query_args); // If there are products if ($q->have_posts()) { // Print the widget wrapper & title echo $before_widget; if ($title) { echo $before_title . $title . $after_title; } // Open the list echo '<ul class="product_list_widget">'; // Print out each product while ($q->have_posts()) { $q->the_post(); // Get a new fflcommerce_product instance $_product = new fflcommerce_product(get_the_ID()); echo '<li>'; // Print the product image & title with a link to the permalink echo '<a href="' . esc_attr(get_permalink()) . '" title="' . esc_attr(get_the_title()) . '">'; // Print the product image echo has_post_thumbnail() ? the_post_thumbnail('shop_tiny') : fflcommerce_get_image_placeholder('shop_tiny'); echo '<span class="js_widget_product_title">' . get_the_title() . '</span>'; echo '</a>'; // Print the price with html wrappers echo '<span class="js_widget_product_price">' . $_product->get_price_html() . '</span>'; echo '</li>'; } echo '</ul>'; // Close the list // Print closing widget wrapper echo $after_widget; // Reset the global $the_post as this query will have stomped on it wp_reset_postdata(); } // Flush output buffer and save to transient cache $cache[$args['widget_id']] = ob_get_flush(); set_transient('fflcommerce_widget_cache', $cache, 3600 * 3); // 3 hours ahead }
/** * Widget * Display the widget in the sidebar * Save output to the cache if empty * * @param array sidebar arguments * @param array instance */ public function widget($args, $instance) { // Start buffering ob_start(); extract($args); // Set the widget title $title = apply_filters('widget_title', $instance['title'] ? $instance['title'] : __('Random Products', 'fflcommerce'), $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' => 'product', 'post_status' => 'publish', 'orderby' => 'rand', 'meta_query' => array(array('key' => 'visibility', 'value' => array('catalog', 'visible'), 'compare' => 'IN'))); // Run the query $q = new WP_Query($query_args); // If there are products if ($q->have_posts()) { // Print the widget wrapper & title echo $before_widget; if ($title) { echo $before_title . $title . $after_title; } // Open the list echo '<ul class="product_list_widget">'; // Print out each product while ($q->have_posts()) { $q->the_post(); // Get new fflcommerce_product instance $_product = new fflcommerce_product(get_the_ID()); echo '<li>'; // Print the product image & title with a link to the permalink echo '<a href="' . get_permalink() . '" title="' . esc_attr(get_the_title()) . '">'; echo has_post_thumbnail() ? the_post_thumbnail('shop_tiny') : fflcommerce_get_image_placeholder('shop_tiny'); echo '<span class="js_widget_product_title">' . get_the_title() . '</span>'; echo '</a>'; // Print the price with html wrappers echo '<span class="js_widget_product_price">' . $_product->get_price_html() . '</span>'; echo '</li>'; } echo '</ul>'; // Close the list // Print closing widget wrapper echo $after_widget; // Reset the global $the_post as this query will have stomped on it wp_reset_postdata(); } ob_get_flush(); }
/** * Widget * Display the widget in the sidebar * Save output to the cache if empty * * @param array sidebar arguments * @param array instance */ public function widget($args, $instance) { // Get the most recent products from the cache $cache = wp_cache_get('widget_recent_products', 'widget'); // If no entry exists use array if (!is_array($cache)) { $cache = array(); } // If cached get from the cache if (isset($cache[$args['widget_id']])) { echo $cache[$args['widget_id']]; return false; } // Start buffering ob_start(); extract($args); // Set the widget title $title = apply_filters('widget_title', $instance['title'] ? $instance['title'] : __('Special Offers', 'fflcommerce'), $instance, $this->id_base); // Set number of products to fetch if (!($number = absint($instance['number']))) { $number = 5; } // Set up query $time = current_time('timestamp'); $query_args = array('posts_per_page' => -1, 'post_type' => 'product', 'post_status' => 'publish', 'orderby' => 'rand', 'meta_query' => array(array('key' => 'visibility', 'value' => array('catalog', 'visible'), 'compare' => 'IN'), array('key' => 'sale_price_dates_from', 'value' => $time, 'compare' => '<='), array('key' => 'sale_price_dates_to', 'value' => $time, 'compare' => '>='))); $q = new WP_Query($query_args); // If there are products if ($q->have_posts()) { // Print the widget wrapper & title echo $before_widget; if ($title) { echo $before_title . $title . $after_title; } // Open the list echo '<ul class="product_list_widget">'; // Print out each product for ($i = 0; $q->have_posts() && $i < $number;) { $q->the_post(); // Get new fflcommerce_product instance $_product = new fflcommerce_product(get_the_ID()); // Skip if not on sale if (!$_product->is_on_sale()) { continue; } else { $i++; } echo '<li>'; // Print the product image & title with a link to the permalink echo '<a href="' . get_permalink() . '" title="' . esc_attr(get_the_title()) . '">'; // Print the product image echo has_post_thumbnail() ? the_post_thumbnail('shop_tiny') : fflcommerce_get_image_placeholder('shop_tiny'); echo '<span class="js_widget_product_title">' . get_the_title() . '</span>'; echo '</a>'; // Print the price with html wrappers echo '<span class="js_widget_product_price">' . $_product->get_price_html() . '</span>'; echo '</li>'; } echo '</ul>'; // Close the list // Print closing widget wrapper echo $after_widget; // Reset the global $the_post as this query will have stomped on it wp_reset_postdata(); } ob_get_flush(); }
function fflcommerce_get_product_thumbnail($size = 'shop_small') { global $post; if (has_post_thumbnail()) { return get_the_post_thumbnail($post->ID, $size); } else { return fflcommerce_get_image_placeholder($size); } }
/** * Widget * Display the widget in the sidebar * Save output to the cache if empty * * @param array sidebar arguments * @param array instance */ function widget($args, $instance) { // Get the most recent products from the cache $cache = wp_cache_get('widget_recent_products', 'widget'); // If no entry exists use array if (!is_array($cache)) { $cache = array(); } // If cached get from the cache if (isset($cache[$args['widget_id']])) { echo $cache[$args['widget_id']]; return false; } // Start buffering ob_start(); extract($args); // Set the widget title $title = $instance['title'] ? $instance['title'] : __('New Products', 'fflcommerce'); $title = apply_filters('widget_title', $title, $instance, $this->id_base); // Set number of products to fetch if (!($number = $instance['number'])) { $number = 10; } $number = apply_filters('fflcommerce_widget_recent_default_number', $number, $instance, $this->id_base); // Set up query $query_args = array('posts_per_page' => $number, 'post_type' => 'product', 'post_status' => 'publish', 'orderby' => 'date', 'order' => 'desc', 'meta_query' => array(array('key' => 'visibility', 'value' => array('catalog', 'visible'), 'compare' => 'IN'))); // Show variations of products? TODO: fix this -JAP- /* if( ! $instance['show_variations']) { $query_args['meta_query'] = array( array( 'key' => 'visibility', 'value' => array('catalog', 'visible'), 'compare' => 'IN', ), ); $query_args['parent'] = false; } */ // Run the query $q = new WP_Query($query_args); // If there are products if ($q->have_posts()) { // Print the widget wrapper & title echo $before_widget; if ($title) { echo $before_title . $title . $after_title; } // Open the list echo '<ul class="product_list_widget">'; // Print out each product while ($q->have_posts()) { $q->the_post(); // Get new fflcommerce_product instance $_product = new fflcommerce_product(get_the_ID()); echo '<li>'; // Print the product image & title with a link to the permalink echo '<a href="' . get_permalink() . '" title="' . esc_attr(get_the_title()) . '">'; echo has_post_thumbnail() ? the_post_thumbnail('shop_tiny') : fflcommerce_get_image_placeholder('shop_tiny'); echo '<span class="js_widget_product_title">' . get_the_title() . '</span>'; echo '</a>'; // Print the price with html wrappers echo '<span class="js_widget_product_price">' . $_product->get_price_html() . '</span>'; echo '</li>'; } echo '</ul>'; // Close the list // Print closing widget wrapper echo $after_widget; // Reset the global $the_post as this query will have stomped on it wp_reset_postdata(); } // Flush output buffer and save to cache $cache[$args['widget_id']] = ob_get_flush(); wp_cache_set('widget_recent_products', $cache, 'widget'); }
/** * Widget * Display the widget in the sidebar * Save output to the cache if empty * * @param array sidebar arguments * @param array instance */ public function widget($args, $instance) { // Get the most recently viewed products from the cache $cache = wp_cache_get('widget_recently_viewed_products', 'widget'); // If no entry exists use array if (!is_array($cache)) { $cache = array(); } // If cached get from the cache if (isset($cache[$args['widget_id']])) { echo $cache[$args['widget_id']]; return false; } // Check if session contains recently viewed products if (empty(fflcommerce_session::instance()->recently_viewed_products)) { return false; } // Start buffering the output ob_start(); extract($args); // Set the widget title $title = apply_filters('widget_title', $instance['title'] ? $instance['title'] : __('Recently Viewed Products', 'fflcommerce'), $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' => 'product', 'post_status' => 'publish', 'nopaging' => true, 'post__in' => fflcommerce_session::instance()->recently_viewed_products, 'orderby' => 'date', 'meta_query' => array(array('key' => 'visibility', 'value' => array('catalog', 'visible'), 'compare' => 'IN'))); // Run the query $q = new WP_Query($query_args); if ($q->have_posts()) { // Print the widget wrapper & title echo $before_widget; if ($title) { echo $before_title . $title . $after_title; } // Open the list echo '<ul class="product_list_widget recently_viewed_products">'; // Print out each produt while ($q->have_posts()) { $q->the_post(); // Get new fflcommerce_product instance $_product = new fflcommerce_product(get_the_ID()); echo '<li>'; //print the product title with a permalink echo '<a href="' . get_permalink() . '" title="' . esc_attr(get_the_title()) . '">'; // Print the product image echo has_post_thumbnail() ? the_post_thumbnail('shop_tiny') : fflcommerce_get_image_placeholder('shop_tiny'); echo '<span class="js_widget_product_title">' . get_the_title() . '</span>'; echo '</a>'; // Print the price with wrappers ..yum! echo '<span class="js_widget_product_price">' . $_product->get_price_html() . '</span>'; echo '</li>'; } echo '</ul>'; // Close the list // Print closing widget wrapper echo $after_widget; // Reset the global $the_post as this query will have stomped on it wp_reset_postdata(); } // Flush output buffer and save to cache $cache[$args['widget_id']] = ob_get_flush(); wp_cache_set('widget_recent_products', $cache, 'widget'); }
/** * Widget * Display the widget in the sidebar * Save output to the cache if empty * * @param array sidebar arguments * @param array instance */ public function widget($args, $instance) { // Get the most recent products from the cache $cache = wp_cache_get('widget_recent_products', 'widget'); // If no entry exists use array if (!is_array($cache)) { $cache = array(); } // If cached get from the cache if (isset($cache[$args['widget_id']])) { echo $cache[$args['widget_id']]; return false; } // Start buffering ob_start(); extract($args); // Set the widget title $title = apply_filters('widget_title', $instance['title'] ? $instance['title'] : __('Top Rated Products', 'fflcommerce'), $instance, $this->id_base); // Set number of products to fetch if (!($number = absint($instance['number']))) { $number = 5; } // Set up query // Filter the $wpdb query add_filter('posts_clauses', array($this, 'order_by_rating')); // TODO: Only display products that are in stock $query_args = array('posts_per_page' => $number, 'post_type' => 'product', 'post_status' => 'publish', 'meta_query' => array(array('key' => 'visibility', 'value' => array('catalog', 'visible'), 'compare' => 'IN'))); // Run the query $q = new WP_Query($query_args); // If there are products if ($q->have_posts()) { // Print the widget wrapper & title echo $before_widget; if ($title) { echo $before_title . $title . $after_title; } // Open the list echo '<ul class="product_list_widget">'; // Print out each product while ($q->have_posts()) { $q->the_post(); $_product = new fflcommerce_product($q->post->ID); echo '<li>'; // Print the title with a link to the permalink echo '<a href="' . esc_url(get_permalink()) . '" title="' . esc_attr(get_the_title()) . '">'; // Print the product image echo has_post_thumbnail() ? the_post_thumbnail('shop_tiny') : fflcommerce_get_image_placeholder('shop_tiny'); echo '<span class="js_widget_product_title">' . get_the_title() . '</span>'; echo '</a>'; // Print the average rating with html wrappers echo $_product->get_rating_html('sidebar'); // Print the price with html wrappers echo '<span class="js_widget_product_price">' . $_product->get_price_html() . '</span>'; echo '</li>'; } echo '</ul>'; // Close the list // Print closing widget wrapper echo $after_widget; // Reset the global $the_post as this query will have stomped on it wp_reset_postdata(); remove_filter('posts_clauses', array($this, 'order_by_rating')); } // Flush output buffer and save to cache $cache[$args['widget_id']] = ob_get_flush(); wp_cache_set('widget_recent_products', $cache, 'widget'); }
/** * Widget * Display the widget in the sidebar * Save output to the cache if empty * * @param array sidebar arguments * @param array instance */ public function widget($args, $instance) { // Get the most recent products from the cache $cache = wp_cache_get('widget_recent_reviews', 'widget'); // If no entry exists use array if (!is_array($cache)) { $cache = array(); } // If cached get from the cache if (isset($cache[$args['widget_id']])) { echo $cache[$args['widget_id']]; return false; } // Start buffering ob_start(); extract($args); // Set the widget title $title = apply_filters('widget_title', $instance['title'] ? $instance['title'] : __('Recent Reviews', 'fflcommerce'), $instance, $this->id_base); // Set number of products to fetch if (!($number = absint($instance['number']))) { $number = 5; } // Modify get_comments query to only include products which are visible add_filter('comments_clauses', array($this, 'where_product_is_visible')); // Get the latest reviews $comments = get_comments(array('number' => $number, 'status' => 'approve', 'post_status' => 'publish', 'post_type' => 'product')); // If there are products if ($comments) { // Print the widget wrapper & title echo $before_widget; if ($title) { echo $before_title . $title . $after_title; } // Open the list echo '<ul class="product_list_widget">'; // Print out each product foreach ($comments as $comment) { // Get new fflcommerce_product instance $_product = new fflcommerce_product($comment->comment_post_ID); // Skip products that are invisible if ($_product->visibility == 'hidden') { continue; } // TODO: Refactor this // Apply star size $star_size = apply_filters('fflcommerce_star_rating_size_recent_reviews', 16); $rating = get_comment_meta($comment->comment_ID, 'rating', true); echo '<li>'; // Print the product image & title with a link to the permalink echo '<a href="' . esc_url(get_comment_link($comment->comment_ID)) . '">'; // Print the product image echo has_post_thumbnail($_product->id) ? get_the_post_thumbnail($_product->id, 'shop_tiny') : fflcommerce_get_image_placeholder('shop_tiny'); echo '<span class="js_widget_product_title">' . $_product->get_title() . '</span>'; echo '</a>'; // Print the star rating echo "<div class='star-rating' title='{$rating}'>\n\t\t\t\t\t\t<span style='width:" . $rating * $star_size . "px;'>{$rating} " . __('out of 5', 'fflcommerce') . "</span>\n\t\t\t\t\t</div>"; // Print the author printf(_x('by %1$s', 'author', 'fflcommerce'), get_comment_author($comment->comment_ID)); echo '</li>'; } echo '</ul>'; // Close the list // Print closing widget wrapper echo $after_widget; // Remove the filter on comments to stop other queries from being manipulated remove_filter('comments_clauses', array($this, 'where_product_is_visible')); } // Flush output buffer and save to cache $cache[$args['widget_id']] = ob_get_flush(); wp_cache_set('widget_recent_reviews', $cache, 'widget'); }
/** * Get the main product image or parents image * * @param string $size * @return string HTML */ public function get_image($size = 'shop_thumbnail') { // Get the image size $size = fflcommerce_get_image_size($size); // If product has an image if (has_post_thumbnail($this->ID)) { return get_the_post_thumbnail($this->ID, $size); } // If product has a parent and that has an image display that if (($parent_ID = wp_get_post_parent_id($this->ID)) && has_post_thumbnail($parent_ID)) { return get_the_post_thumbnail($this->ID, $size); } // Otherwise just return a placeholder return fflcommerce_get_image_placeholder($size); }