WC_AJAX::json_search_categories()public staticWC 1.0

Search for categories and return json.

Method of the class: WC_AJAX{}

Return

null. Nothing (null).

Usage

$result = WC_AJAX::json_search_categories();

WC_AJAX::json_search_categories() code WC 8.7.0

public static function json_search_categories() {
	ob_start();

	check_ajax_referer( 'search-categories', 'security' );

	if ( ! current_user_can( 'edit_products' ) ) {
		wp_die( -1 );
	}

	$search_text = isset( $_GET['term'] ) ? wc_clean( wp_unslash( $_GET['term'] ) ) : '';

	if ( ! $search_text ) {
		wp_die();
	}

	$show_empty       = isset( $_GET['show_empty'] ) ? wp_validate_boolean( wc_clean( wp_unslash( $_GET['show_empty'] ) ) ) : false;
	$found_categories = array();
	$args             = array(
		'taxonomy'   => array( 'product_cat' ),
		'orderby'    => 'id',
		'order'      => 'ASC',
		'hide_empty' => ! $show_empty,
		'fields'     => 'all',
		'name__like' => $search_text,
	);

	$terms = get_terms( $args );

	if ( $terms ) {
		foreach ( $terms as $term ) {
			$term->formatted_name = '';

			$ancestors = array();
			if ( $term->parent ) {
				$ancestors = array_reverse( get_ancestors( $term->term_id, 'product_cat' ) );
				foreach ( $ancestors as $ancestor ) {
					$ancestor_term = get_term( $ancestor, 'product_cat' );
					if ( $ancestor_term ) {
						$term->formatted_name .= $ancestor_term->name . ' > ';
					}
				}
			}

			$term->parents                      = $ancestors;
			$term->formatted_name              .= $term->name . ' (' . $term->count . ')';
			$found_categories[ $term->term_id ] = $term;
		}
	}

	wp_send_json( apply_filters( 'woocommerce_json_search_found_categories', $found_categories ) );
}