WC_Admin_List_Table_Products::render_products_category_filter()protectedWC 3.5.0

Render the product category filter for the list table.

Method of the class: WC_Admin_List_Table_Products{}

Return

null. Nothing (null).

Usage

// protected - for code of main (parent) or child class
$result = $this->render_products_category_filter();

Changelog

Since 3.5.0 Introduced.

WC_Admin_List_Table_Products::render_products_category_filter() code WC 9.4.2

<?php
protected function render_products_category_filter() {
	$categories_count = (int) wp_count_terms( 'product_cat' );

	if ( $categories_count <= apply_filters( 'woocommerce_product_category_filter_threshold', 100 ) ) {
		wc_product_dropdown_categories(
			array(
				'option_select_text' => __( 'Filter by category', 'woocommerce' ),
				'hide_empty'         => 0,
			)
		);
	} else {
		$current_category_slug = isset( $_GET['product_cat'] ) ? wc_clean( wp_unslash( $_GET['product_cat'] ) ) : false; // WPCS: input var ok, CSRF ok.
		$current_category      = $current_category_slug ? get_term_by( 'slug', $current_category_slug, 'product_cat' ) : false;
		?>
		<select class="wc-category-search" name="product_cat" data-placeholder="<?php esc_attr_e( 'Filter by category', 'woocommerce' ); ?>" data-allow_clear="true">
			<?php if ( $current_category_slug && $current_category ) : ?>
				<option value="<?php echo esc_attr( $current_category_slug ); ?>" selected="selected"><?php echo esc_html( htmlspecialchars( wp_kses_post( $current_category->name ) ) ); ?></option>
			<?php endif; ?>
		</select>
		<?php
	}
}