Introduction
Enhance the functionality of Smart WooCommerce Search by using custom hooks and filters to modify search behavior and results.
Hooks
Smart Search Widget Settings Saved
Execute PHP code when the Smart Search widget settings are saved. The variable $widget_id can be ‘product’, ‘default’, ‘avada’, or a numeric ID of a custom widget.
add_action( 'sws_widget_settings_saved', 'my_sws_widget_settings_saved', 20, 1 ); /** * Actions after widget settings saved * * @param string|int $widget_id Search widget id. */ function my_sws_widget_settings_saved( $widget_id ) { wp_cache_delete( 'my_cache_var', 'my_cache_group' ); }
Filters
Search Terms List
Modify the array of search terms.
add_filter( 'sws_filter_search_terms', 'my_filter_search_terms', 50, 1 ); /** * Modify the array of search terms * * @param array $search_terms List of search terms. */ function my_filter_search_terms( $search_terms ) { // modify the array of search terms return $search_terms; }
Search Results Post IDs
Modify the array of post IDs in search results.
add_filter( 'sws_search_result_post_ids', 'my_search_result_post_ids', 10, 1 ); /** * Modify the array of post ids * * @param array $post_ids List of post ids. */ function my_search_result_post_ids( $post_ids ) { // modify the array of post ids return $post_ids; }
Suggestion Thumbnail Output
Customize the HTML output for suggestion thumbnails in the search results popup.
add_filter( 'sws_suggestion_image_output', 'my_suggestion_image_output', 10, 2 ); /** * Overwrite image html in the suggestion * * @param string $output Image html output. * @param \WP_Post|\WC_Product $the_post WordPress post object or WooCommerce product object */ function my_suggestion_image_output( $output, $the_post ) { $output = get_the_post_thumbnail( $the_post, 'medium' ); return $output; }
“Add to Cart” Button Output
Customize the “Add to Cart” button HTML in the search results popup.
add_filter( 'sws_suggestion_add_to_cart_button_output', 'my_suggestion_add_to_cart_button_output', 10, 2 ); /** * Pre define "Add to cart" button html in the suggestion * * @param string $output Button output - initially empty string. * @param \WC_Product $product WordPress post object or WooCommerce product object */ function my_suggestion_add_to_cart_button_output( $output, $product ) { $output = '.....'; return $output; }
“View All” Button Text
Modify the text in the “View All” button in search results popup.
add_filter( 'sws_view_all_button_text', 'my_view_all_text', 10, 2 ); /** * Modify text in the "View All" button * * @param string $button_text Text in the button. * @param int $found_posts Number of found posts. */ function my_view_all_text( $button_text, $found_posts ) { $button_text = 'View all products'; return $button_text; }
WooCommerce Filters
Single Search Result Redirect
WooCommerce redirects to the product page if there is only one search result. Disable this by adding the following code to your functions.php
file in your child theme or use a plugin for custom functions (such as the Code snippets plugin).
add_filter( 'woocommerce_redirect_single_search_result', '__return_false' );
Alternatively, disable this feature in the Smart Search settings under the “General” tab by activating the “Disable Redirect to Product Page” toggle.
Number of Products
Adjust the number of products displayed per page in search results page. Add the following code to your child theme’s functions.php
file or use a plugin for custom functions.
add_filter( 'loop_shop_per_page', 'new_loop_shop_per_page', 20 ); function new_loop_shop_per_page( $cols ) { // Return the number of products you want to show per page. if ( is_search() ) { $cols = 12; } return $cols; }
Code example sets 12 products per page.
The post Hooks / Filters appeared first on Smart WooCommerce Search.