'jigoshop_random_products', 'description' => __( 'Lists a random selection of products on your site.', 'jigoshop' ), ); // Create the widget parent::__construct( 'jigoshop_random_products', __('Jigoshop: Random Products', 'jigoshop' ), $options ); } /** * 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', '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' => '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; echo $before_title . $title . $after_title; // Open the list echo ''; // 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(); } /** * Update * * @param array new instance * @param array old instance * @return array instance */ public function update( $new_instance, $old_instance ) { $instance = $old_instance; // Save the new values $instance['title'] = strip_tags($new_instance['title']); $instance['number'] = abs($new_instance['number']); return $instance; } /** * Form * * Displays the form for the wordpress admin * * @param array instance */ function form( $instance ) { // Get instance data $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : null; $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5; // Widget Title echo "

"; // Number of posts to fetch echo "

"; } } // class Jigoshop_Widget_Random_Products