GET FREE VERSION GET THE PRO VERSION

How to add custom data inside search results

Learn how to add extra data to the product that is shown inside the search results block.

In this article

 

Plugin has some options to choose what data to show inside search results items. Via plugin settings page you can choose whether to show or not the product image, price, tags and categories, sale badge, stock status, etc. Read more about search results customization.

But if you need more control of what data to display than this tutorial will provide some examples of such cases. In all examples we will use plugin php filters that gives great control of what product data to show for search results block.

Showing product custom taxonomies

Product categories and tags are displayed inside search results items by default. But if we want to display any custom product taxonomies terms then we need to use some custom code for this.

For example, products have a custom taxonomy called product_brand and we need to display these taxonomy terms names for each product inside the search results output.
Lets display after product short description and set brands text color to #330ced.

Custom taxonomy display inside the plugin search results

Custom taxonomy display inside the plugin search results

add_filter( 'aws_excerpt_search_result', 'my_aws_excerpt_search_result', 10, 2 );
function my_aws_excerpt_search_result( $excerpt, $post_id ) {
    $brands = get_the_terms( $post_id, 'product_brand' );
    $all_brands = array();
    if ( ! is_wp_error( $brands ) && ! empty( $brands ) ) {
        foreach ( $brands as $brand ) {
            $all_brands[] = $brand->name;
        }
        $excerpt = $excerpt . '<br>' . '<span style="margin-top: 3px;display: block;color: #330ced;">Brands: ' . implode( ',', $all_brands ) . '</span>';
    }
    return $excerpt;
}

Showing product custom fields

Another possible option - displaying specific product custom field value inside the search results list.

For example, each product has custom field vin_number and we want to display this field value. Let's display it right under the product name.

Display product custom field inside the plugin search results

Display product custom field inside the plugin search results

add_filter( 'aws_excerpt_search_result', 'aws_excerpt_search_result', 10, 2 );
function aws_excerpt_search_result( $excerpt, $post_id ) {
    $field_name = 'vin_number';
    $custom_fields = get_post_custom( $post_id );
    if ( $custom_fields && ! empty( $custom_fields ) && isset( $custom_fields[$field_name] ) ) {
        $excerpt = '<span style="display:block;margin-bottom:4px;font-weight:bold;">VIN: ' . $custom_fields[$field_name][0] . '</span>' . $excerpt;
    }
    return $excerpt;
}

Showing field created via Advanced Custom Field (ACF) plugin

If you are using the Advanced Custom fields plugin then it is possible to display this fields value for your search results. For example, we have a text field manufacturer and want to display it after the product short description.

Product custom field created via ACF plugin

Product custom field created via ACF plugin

Display product ACF field  value inside the plugin search results

Display product ACF field value inside the plugin search results

add_filter( 'aws_excerpt_search_result', 'my_aws_excerpt_search_result', 10, 2 );
function my_aws_excerpt_search_result( $excerpt, $post_id ) {
    $field_name = 'manufacturer';
    if ( function_exists( 'get_field' ) ) {
        $field = get_field( $field_name, $post_id );
        if ( $field ) {
            $excerpt = $excerpt . '<br>' . '<span style="margin-top:3px;display:block;color:#000;font-size:11px;font-weight:bold;">Manufacturer : ' . $field . '</span>';
        }
    }
    return $excerpt;
}

Advanced Woo Search also supports searching via ACF plugin field. Read more here.

Showing product shipping class

Another possible option - displaying product shipping class for each item inside the search results block.

Display product shipping class inside the plugin search results

Display product shipping class inside the plugin search results

add_filter( 'aws_excerpt_search_result', 'my_aws_excerpt_search_result', 10, 3 );
function my_aws_excerpt_search_result( $excerpt, $post_id, $product ) {
    $class_id = $product->get_shipping_class_id();
    if ( $class_id ) {
        $term = get_term_by( 'id', $class_id, 'product_shipping_class' );
        if ( $term && ! is_wp_error( $term ) ) {
            $excerpt = $excerpt . '<br>' . $term->name;
        }
    }
    return $excerpt;
}

Showing product weight and dimensions

Lets show weight and dimensions for those products that have these options.

Display product weight and dimensions inside the plugin search results

Display product weight and dimensions inside the plugin search results

add_filter( 'aws_excerpt_search_result', 'my_aws_excerpt_search_result', 10, 3 );
function my_aws_excerpt_search_result( $excerpt, $post_id, $product ) {
    if ( $product->has_weight() ) {
        $weight_unit = strtolower( get_option( 'woocommerce_weight_unit' ) );
        $excerpt .= '<br>Weight: ' . $product->get_weight() . ' ' . $weight_unit;
    }
    if ( $product->has_dimensions() ) {
        $excerpt .= '<br>Dimensions: ' . $product->get_dimensions( true );
    }
    return $excerpt;
}

Showing product attributes

For example we have several products with size and color attributes and want to show these values inside search results output.

Note: these attributes will be shown only for simpleexternal and child products. If you want to show attributes for variable products then such features are available by default. You can also check it inside the plugin settings page -> Show variations attributes option.
Display product attributes inside the plugin search results

Display product attributes inside the plugin search results

add_filter( 'aws_excerpt_search_result', 'my_aws_excerpt_search_result', 10, 3 );
function my_aws_excerpt_search_result( $excerpt, $post_id, $product ) {
    $attributes_to_show = array( 'pa_size' => 'Size', 'pa_color' => 'Color' );
    $attributes = $product->get_attributes();
    $attrs = array();
    if ( $product->is_type( 'simple' ) || $product->is_type( 'external' ) || $product->is_type( 'variation' ) ) {
        if ( $attributes ) {
            foreach ( $attributes as $attribute_slug => $attribute_values ) {
                if ( isset( $attributes_to_show[$attribute_slug] ) ) {
                    $attr_name = $attributes_to_show[$attribute_slug];
                    if ( is_object( $attribute_values ) ) {
                        $terms = $attribute_values->get_terms();
                        if ( $terms ) {
                            foreach( $terms as $term ) {
                                $attrs[$attr_name][] = $term->name;
                            }
                        }
                    } else {
                        $attrs[$attr_name][] = $attribute_values;
                    }

                }
            }
        }
    }
    if ( ! empty( $attrs ) ) {
        foreach( $attrs as $attr_name => $attr_terms ) {
            $excerpt .=  '<br>' . $attr_name . ' ';
            foreach ( $attr_terms as $attr_term ) {
                $excerpt .= '<span style="display: inline-block;padding: 0 5px;border: 1px solid #ccc;margin: 5px 5px 2px 0;">' . $attr_term . '</span>';
            }
        }
    }

    return $excerpt;
}

Showing the product author

We can display the name of the user who created the product for each search results item.

Display the product author name inside the plugin search results

Display the product author name inside the plugin search results

add_filter( 'aws_excerpt_search_result', 'my_aws_excerpt_search_result', 10, 2 );
function my_aws_excerpt_search_result( $excerpt, $post_id ) {
    $post_data = get_post( $post_id );
    $author_meta = get_the_author_meta( 'user_nicename', $post_data->post_author );
    if ( $author_meta ) {
        $excerpt = $excerpt . '<br>' . '<span style="margin-top:5px;display:block;color:#00ae14;font-size:11px;">' . $author_meta . '</span>';

    }
    return $excerpt;
}

Showing product ID

Lets add product ID number to the end of each product's names for our search results list.

Display product ID inside the plugin search results

Display product ID inside the plugin search results

add_filter( 'aws_title_search_result', 'my_aws_title_search_result', 10, 2 );
function my_aws_title_search_result( $title, $post_id ) {
    $title .= '<span style="color:#000;font-size:11px;font-weight:bold;">. ID: ' . $post_id . '</span>';
    return $title;
}

Showing product sales number

Display product total sales count after the short description text.

Display product sales count inside the plugin search results

Display product sales count inside the plugin search results

add_filter( 'aws_excerpt_search_result', 'my_aws_excerpt_search_result', 10, 3 );
function my_aws_excerpt_search_result( $excerpt, $post_id, $product ) {
    $value = method_exists( $product, 'get_total_sales' ) ? $product->get_total_sales() : get_post_meta( $post_id, 'total_sales', true );
    if ( $value ) {
        $excerpt = $excerpt . '<br>' . '<span style="margin-top:3px;display:block;color:#000;font-size:11px;font-weight:bold;">Total sales : ' . $value . '</span>';
    }
    return $excerpt;
}