WC_Widget_Top_Rated_Products::widget │ public │ WC 1.0

Output widget.

Method of the class: WC_Widget_Top_Rated_Products{}

Returns

null. Nothing (null).

Usage

$WC_Widget_Top_Rated_Products = new WC_Widget_Top_Rated_Products();
$WC_Widget_Top_Rated_Products->widget( $args, $instance );
$args(array) (required)
Arguments.
$instance(array) (required)
Widget instance.

Notes

See: WP_Widget

WC_Widget_Top_Rated_Products::widget() code WC 11.2.1

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'];

	$query_args = apply_filters(
		'woocommerce_top_rated_products_widget_args',
		array(
			'posts_per_page' => $number,
			'no_found_rows'  => 1,
			'post_status'    => 'publish',
			'post_type'      => 'product',
			// Left in the args so that the filter above keeps seeing the documented payload; the
			// query itself is switched to wc_product_meta_lookup in query_top_rated_products().
			'meta_key'       => '_wc_average_rating', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- Part of the filter payload only; query_top_rated_products() orders through wc_product_meta_lookup instead of joining post meta.
			'orderby'        => 'meta_value_num',
			'order'          => 'DESC',
			'meta_query'     => WC()->query->get_meta_query(), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Empty unless an extension adds clauses through woocommerce_product_query_meta_query; the same container the shop catalog query uses.
			'tax_query'      => WC()->query->get_tax_query(), // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query -- Product visibility exclusions are indexed by term_taxonomy_id and are the only way to hide catalog-excluded products.
		)
	);

	$r = $this->query_top_rated_products( $query_args );

	if ( $r->have_posts() ) {

		$this->widget_start( $args, $instance );

		echo wp_kses_post( apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' ) );

		$template_args = array(
			'widget_id'   => isset( $args['widget_id'] ) ? $args['widget_id'] : $this->widget_id,
			'show_rating' => true,
		);

		while ( $r->have_posts() ) {
			$r->the_post();
			wc_get_template( 'content-widget-product.php', $template_args );
		}

		echo wp_kses_post( apply_filters( 'woocommerce_after_widget_product_list', '</ul>' ) );

		$this->widget_end( $args );
	}

	wp_reset_postdata();

	$content = ob_get_clean();

	echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Buffered widget markup; each dynamic value is escaped or annotated where it is rendered.

	$this->cache_widget( $args, $content );
}