/** * widget function. * * @see WP_Widget * * @param array $args * @param array $instance * * @return void * * @since 1.0.0 */ public function widget($args, $instance) { if ($this->get_cached_widget($args)) { return; } ob_start(); $number = !empty($instance['number']) ? absint($instance['number']) : $this->settings['number']['std']; $count_key = 'wcmvp_product_view_count'; $query_args = array('posts_per_page' => $number, 'no_found_rows' => 1, 'post_status' => 'publish', 'post_type' => 'product', 'orderby' => 'meta_value_num', 'order' => 'DESC', 'meta_key' => $count_key); $query_args['meta_query'] = array(array('key' => $count_key, 'value' => '0', 'type' => 'numeric', 'compare' => '>')); $r = new WP_Query($query_args); if ($r->have_posts()) { $this->widget_start($args, $instance); echo '<ul class="product_list_widget">'; while ($r->have_posts()) { $r->the_post(); global $product; ?> <li> <a href="<?php echo esc_url(get_permalink($product->id)); ?> " title="<?php echo esc_attr($product->get_title()); ?> "> <?php echo $product->get_image(); ?> <span class="product-title"><?php echo $product->get_title(); ?> </span> </a> <?php echo wcmvp_get_view_count_html($product->id); ?> <?php echo $product->get_price_html(); ?> </li> <?php } echo '</ul>'; $this->widget_end($args); } else { echo '<ul class="product_list_widget">'; echo '<li>' . __('No products have been viewed yet !!', 'woo-most-viewed-products') . '</li>'; echo '</ul>'; } wp_reset_postdata(); $content = ob_get_clean(); echo $content; $this->cache_widget($args, $content); }
/** * @param int $num_posts * * @return string * * @since 1.0.0 */ function wcmvp_render_most_viewed_products($num_posts = 10) { $r = wcmvp_get_most_viewed_products($num_posts); ob_start(); if ($r->have_posts()) { echo '<ul class="woo-most-viewed product_list_widget">'; while ($r->have_posts()) { $r->the_post(); global $product; ?> <li> <a href="<?php echo esc_url(get_permalink($product->id)); ?> " title="<?php echo esc_attr($product->get_title()); ?> "> <?php echo $product->get_image(); ?> <span class="product-title"><?php echo $product->get_title(); ?> </span> </a> <?php echo wcmvp_get_view_count_html($product->id); ?> <?php echo $product->get_price_html(); ?> </li> <?php } echo '</ul>'; } else { echo '<ul class="woo-most-viewed wcmvp-not-found product_list_widget">'; echo '<li>' . __('No products have been viewed yet !!', 'woo-most-viewed-products') . '</li>'; echo '</ul>'; } wp_reset_postdata(); $content = ob_get_clean(); return $content; }