Forums › Forums › Search & Filter Pro › Confine Search & Filter to Current Woocommerce Category Page
Tagged: category, woocommerce
- This topic has 2 replies, 2 voices, and was last updated 8 years, 8 months ago by Anonymous.
-
Anonymous(Private) March 14, 2016 at 6:00 pm #39403
Hello there,
I apologize if this has been asked before. I did some searching on these forums and thought I had found the start of a solution but it’s not working and I don’t know if it’s because I have the code wrong or the whole approach wrong. My situation:
I have a shop page that I’m building for a customer that has multiple product categories and subcategories. On the woocommerce archive page, I have a sidebar with a search and filter form that allows product search, category multi-select, price range, and sort order. It works excellent for ALL products, but I’m afraid the end user will get confused when they navigate to a product category via the main navigation, then try to use the search and filter form to search / filter JUST that category. Anytime you use the form it searches all products, which redirects to the main shop page.
My searches led me to the Edit Query Args function, which suggests that I could alter the function to have a conditional statement for whether or not the page was a product category, then filter the arguments for just that category. Here’s my code:
function ag_filter_category_search( $query_args, $sfid ) { if ( is_product_category() ) { $category = get_queried_object(); $cat_id = $category->term_id; //if search form ID = 225, the do something with this query if($sfid==4852) { $query_args = array ( 'product_cat' => $cat_id, ); } return $query_args; } } add_filter( 'sf_edit_query_args', 'ag_filter_category_search', 10, 2 );
This is currently not doing anything that I can see, but it might be because the code itself is wrong. What I need to accomplish if possible (two options?):
1. When the user clicks to a category or subcategory page, have that automatically get added to the S&F form under the multiselect categories field. This way any further searches or filters will honor the current category (and the results will STAY on that category page rather than redirecting to the base “shop” page).
2. When the user is on a category page, change the S&F form to change the search query arguments to restrict results only to that category (and not leave the category page). Could this be something like filtering the URL that comes out of the search form? For example:
http://mysite.com/shop/?_sft_search_term=blahblah could be changed to:
http://mysite.com/product-category/category-name/?_sft_search_term=blahblahThe reason I’d not like to leave the category page is there is a custom display for each category that includes a category feature image and description.
Ross Moderator(Private) March 16, 2016 at 2:50 pm #39534Hey Jake
I think you can go with option 1)
Enabled the option “detect defaults from current page” – found under the “general” tab – this should preselect your inputs with the correct product cats/tags
Option 2) is actually something I’m working on for a future update 🙂
Thanks
-
AuthorPosts