How to display extra text in search suggestions?

The solution works only for the Pro plugin version.

In the search suggestion, you can display a few things: product name, image, SKU, description, and price. Sometimes, however, this is not enough. If you sell wine, you may want to display a producer. If you sell books, you may want to display the author.

The following guide introduces a few tricks on how to display extra things, e.g. custom fields, attributes, or custom taxonomies in the search suggestions.

Required steps before implementing the code

Regardless of what you want to display, you must do two things:

Enable the option “Show product description”.

Go to WooCommerce -> AJAX Search bar -> Autocomplete (tab). Click “Show advanced settings” and enable the option “Show product description”. All extra data will be displayed instead of a description.

Implementation

Below you will see samples of snippets. Here is a way how to implement these snippets. It’s not a default way you can know from WordPress. You must use a child-theme.

  1. Create a new empty file asfw-filters.php in the root directory of your child-theme
  2. Edit this file and paste there one of the snippets you will see below.

Snippets

There are 3 snippets you can use for different cases. Use it as samples. Remember to replace the taxonomy name or custom field.

Display book authors (custom taxonomy)

The book store wants to display authors in the search suggestion. They created custom taxonomy “book_author” and assigned books to the terms of this taxonomy.

<?php

add_filter( 'dgwt/wcas/tnt/search_results/output', function ( $results ) {

	$taxonomy_name = 'book_author'; // Replace with yours!!!

	if ( ! empty( $results['suggestions'] ) ) {
		$i = 0;
		foreach ( $results['suggestions'] as $suggestion ) {
			if ( isset( $suggestion['post_id'] ) ) {

				global $wpdb;

				$postID                               = absint( $suggestion['post_id'] );
				$results['suggestions'][ $i ]['desc'] = '';

				$sql = $wpdb->prepare( "SELECT GROUP_CONCAT( t.name SEPARATOR ', ')
                             FROM $wpdb->terms AS t
                             INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
                             INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id
                             WHERE tt.taxonomy = %s
                             AND tr.object_id = %d", $taxonomy_name, $postID );


				$author = $wpdb->get_var( $sql ); // Get extra label value

				if ( ! empty( $author ) ) {

					// Print author instead of description field (new line)
					$results['suggestions'][ $i ]['desc'] = $author;
				}

			}

			$i ++;
		}
	}

	return $results;
} );

Display EAN code (custom field)

<?php

add_filter( 'dgwt/wcas/tnt/search_results/output', function ( $results ) {

	$meta_key = 'ean'; // Replace with yours custom field!!!

	if ( ! empty( $results['suggestions'] ) ) {
		$i = 0;
		foreach ( $results['suggestions'] as $suggestion ) {
			if ( isset( $suggestion['post_id'] ) ) {

				global $wpdb;

				$postID                               = absint( $suggestion['post_id'] );
				$results['suggestions'][ $i ]['desc'] = '';

				$sql = $wpdb->prepare( "SELECT meta_value
                                       FROM $wpdb->postmeta
                                       WHERE post_id = %s
                                       AND meta_key = %s LIMIT 1",
					$postID, $meta_key );


				$ean = $wpdb->get_var( $sql ); // Get extra label value

				if ( ! empty( $ean ) ) {

					// Print EAN instead of description field (new line)
					$results['suggestions'][ $i ]['desc'] = $ean;

				}

			}

			$i ++;
		}
	}

	return $results;
} );

Display attribute

Attributes in WordPress are taxonomies. If you want to display an attribute in the search suggestions, take a look at the custom taxonomy solution described above.