Forums › Forums › Search & Filter Pro › Woocommerce variation filter
- This topic has 27 replies, 3 voices, and was last updated 5 years, 2 months ago by
Ross.
-
Ross Moderator(Private) July 6, 2020 at 3:34 pm #251556
Hi Jordi
It can be very difficult looking at data you are unfamiliar with, and in another language! So I may ask more questions than usual.
I just disabled the restriction of
pa_perro-gato
– does the field show the correct options? (of course, now cats are shown too, but I just want to check the options that are generated in the field)Also, as in my last post, can you setup WPML with S&F properly? This could be affecting things – https://searchandfilter.com/documentation/3rd-party/wpml/
Thanks
Anonymous(Private) July 7, 2020 at 12:03 pm #251684Hi again, not worry, i glad to answer your quetions if is helping to fix this.
Yes, is working if remove this setting. But we want to filter the search. That’s why we use this restriction.We want:
Using this category: pa_perro-gato, can filter between cats and dogs. We can use also product_cat that’s the deafult woo category. How can do this?
All products have set this categories.Thanks!
Ross Moderator(Private) July 7, 2020 at 12:31 pm #251714Hi Jordi
Ok I think I know whats going on, essentially, the
tags, categories & taxonomies
tab, is a fancy UI for WP_Query – https://developer.wordpress.org/reference/classes/wp_query/When you put this restriction, it does not properly take into consideration the variations.
To get around this, we need use some custom code – how are you with using WP hooks / filters – if I give you some code + instruction, will you be able to make some small modifications and add to your child theme?
Thanks
Ross Moderator(Private) July 7, 2020 at 12:36 pm #251718Ok great, I’ve put comments in the code so put in you text editor and read through (this should go in your themes
functions.php
) – let me know if any questions://restrict the results to a specific product attribute, by attibute ID add_filter('sf_apply_custom_filter', 'sf_woocommerce_filter_by_attribute', 12, 3); function sf_woocommerce_filter_by_attribute($ids_array, $query_args, $sfid) { // this is the ID of the search form we want to apply the include on, change 123456 to your search form ID if($sfid===123456){ $field_name = "_sft_pa_perro-gato"; //the S&F name for the field - I think this is correct for your setup // here, you need to check, are we on Pero attribute, or are we on Gato attribute, // then set $field_value accordingly $field_value = 999; //the ID of your product attribute that you want to "restrict" the results to // --- thats it, leave the rest of the code in tact //get the visibility IDs directly from S&F tables $merge = true; if(isset($ids_array[0])) { if ( $ids_array[0] === false ) { $merge = false; } } global $wpdb; $term_results_table_name = $wpdb->prefix . 'search_filter_term_results'; $field_terms_results = $wpdb->get_results( " SELECT field_name, field_value, result_ids FROM $term_results_table_name WHERE field_name = '$field_name' AND field_value = '$field_value' LIMIT 0, 1 " ); if((count($field_terms_results)==1) && (property_exists($field_terms_results[0], 'result_ids'))) { $ids = explode(',', $field_terms_results[0]->result_ids); if ( $merge == false ) { $ids_array = $ids; } else { $ids_array = array_intersect( $ids_array, $ids ); } } } return $ids_array; }
Anonymous(Private) July 7, 2020 at 2:43 pm #251799GREAT!
Is working, thanks you very much Ross.
I testes directly here: https://picartpetcare.com/buscador/
I added more code to do the same with the cat’s forms.
And is working :). -
AuthorPosts