Archived topic
Add to cart button in search results
9 replies · Started by BreoganGal on July 8, 2021
Hi, I want to insert Add to cart button in the search results. How can I insert it?
Hi there,
will there only be Products in your search results ?
Yes, I want to show only products
You would normally use the Product Search widget that comes with woocommerce.
But as you're using a search plugin have you checked with their support on how to limit it to products and use the product-archives template ?
It is the theme that controls the output of the results. We only send a list of matching post IDs to the loop. You would need to check with the theme author, their knowledgebase or online forums for how to add content features to the template.
You can try adding this PHP Snippet:
function my_search_filter($query) {
if ( $query->is_search && ! is_admin() ) {
$query->set( 'post_type', 'product' );
$query->is_post_type_archive = true;
}
}
add_filter('pre_get_posts','my_search_filter', 9);
This will filter the results to products and display it using the woo archive template.
It is the theme that controls the output of the results. We only send a list of matching post IDs to the loop. You would need to check with the theme author, their knowledgebase or online forums for how to add content features to the template.
I put this snippet but I don´t see any changes.
I tested the snippet but using the default WP Search Form and it does work.
Can you test it with the default Search Widget ?
Yes, with default search widget works.
I am not sure what the answer is there. You may want to share the code i provided above with the WP Search and Filter Developer, as it works with the WP search then there may be something the plugin is doing differently.