Search results to category or subcategory
-
Good afternoon! I would like to ask if this scenario is doable: I use WooCommerce and Elementor PRO. Can I have a second YMM search bar which on search to redirect the users to a specific category or subcategory?
For example, I have the page ‘alarms’. (on this page I will use the YMM shortcode to display my second search bar). The user will search for BMW 1-Series 2004-2014 (E81-E82-E87-E88) – on search he will be redirected to product-category/alarms and the results will be restricted to that category only.
Tried to add a snippet but no result just yet.
// Redirect WooCommerce search to specific category for YMM selector shortcode function redirect_woocommerce_search_to_category_for_shortcode($query) { // Check if the YMM selector shortcode is present on the page global $post; if (is_search() && !is_admin() && $query->is_main_query() && has_shortcode($post->post_content, 'ymm_selector')) { $search_query = $query->get('s'); $category_slug = 'alarms'; // Replace with your category slug $category = get_term_by('slug', $category_slug, 'product_cat'); if ($category) { $query->set('post_type', 'product'); $query->set('tax_query', array( array( 'taxonomy' => 'product_cat', 'field' => 'term_id', 'terms' => $category->term_id, ), )); $query->set('s', $search_query); // Preserve search query } } } add_action('pre_get_posts', 'redirect_woocommerce_search_to_category_for_shortcode');
Thanks a lot!
Iulian
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Search results to category or subcategory’ is closed to new replies.