id_base = 'woocommerce_random_products'; $this->name = __('WooCommerce Random Products', 'woocommerce' ); $this->widget_options = array( 'classname' => 'widget_random_products', 'description' => __( 'Display a list of random products on your site.', 'woocommerce' ), ); parent::__construct( $this->id_base, $this->name, $this->widget_options ); } /** * widget function. * * @see WP_Widget * @access public * @param array $args * @param array $instance * @return void */ function widget( $args, $instance ) { global $woocommerce; // Use default title as fallback $title = ( '' === $instance['title'] ) ? __('Random Products', 'woocommerce') : $instance['title']; $title = apply_filters('widget_title', $title, $instance, $this->id_base); // Setup product query $query_args = array( 'post_type' => 'product', 'post_status' => 'publish', 'posts_per_page' => $instance['number'], 'orderby' => 'rand', 'no_found_rows' => 1 ); $query_args['meta_query'] = array(); if ( ! $instance['show_variations'] ) { $query_args['meta_query'][] = $woocommerce->query->visibility_meta_query(); $query_args['post_parent'] = 0; } $query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query(); $query = new WP_Query( $query_args ); if ( $query->have_posts() ) { echo $args['before_widget']; if ( '' !== $title ) { echo $args['before_title'], $title, $args['after_title']; } ?> update * @access public * @param array $new_instance * @param array $old_instance * @return array */ function update( $new_instance, $old_instance ) { $instance = array( 'title' => strip_tags($new_instance['title']), 'number' => min(15, max(1, (int) $new_instance['number'])), 'show_variations' => ! empty($new_instance['show_variations']) ); return $instance; } /** * form function. * * @see WP_Widget->form * @access public * @param array $instance * @return void */ function form( $instance ) { // Default values $title = isset( $instance['title'] ) ? $instance['title'] : ''; $number = isset( $instance['number'] ) ? (int) $instance['number'] : 5; $show_variations = ! empty( $instance['show_variations'] ); ?>

/>