Home / Forums / WoodMart support forum / How can I add "filter by brands" filter function in the backend?
Home › Forums › WoodMart support forum › How can I add "filter by brands" filter function in the backend?
How can I add "filter by brands" filter function in the backend?
- This topic has 12 replies, 1 voice, and was last updated 3 years, 9 months ago by
loralora.
-
AuthorPosts
-
May 29, 2019 at 4:14 am #126165
abParticipantHi,
How can I add “filter by brands” filter function in the backend?. I added following code in the function.php. And it does shows all the brands with how many products. But when I try to filter it, it is doing nothing. Please help. What I am i missing here.
// FILTER BY BRANDS - BACKEND add_filter( 'woocommerce_product_filters', 'bbloomer_filter_by_custom_taxonomy_dashboard_products' ); function bbloomer_filter_by_custom_taxonomy_dashboard_products( $output ) { global $wp_query; $output .= wc_product_dropdown_categories( array( 'show_option_none' => 'Filter by Brands', 'taxonomy' => 'pa_brand', 'name' => 'pa_brand', 'selected' => isset( $wp_query->query_vars['pa_brand'] ) ? $wp_query->query_vars['pa_brand'] : '', ) ); return $output; }Attachments:
You must be logged in to view attached files.May 29, 2019 at 4:49 am #126167
abParticipantPlease look at following code;
If I put $taxonomy = ‘product_tag’; it does show the tag filter and I can sort it. But when I put $taxonomy = ‘pa_brand’; – it does show the brand filter listing, but I can’t filter/sort it.add_action('restrict_manage_posts', 'product_tags_sorting'); function product_tags_sorting() { global $typenow; $taxonomy = 'pa_brand'; if ( $typenow == 'product' ) { $selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : ''; $info_taxonomy = get_taxonomy($taxonomy); wp_dropdown_categories(array( 'show_option_all' => __("Show all {$info_taxonomy->label}"), 'taxonomy' => $taxonomy, 'name' => $taxonomy, 'orderby' => 'name', 'selected' => $selected, 'show_count' => true, 'hide_empty' => true, )); }; } add_action('parse_query', 'product_tags_sorting_query'); function product_tags_sorting_query($query) { global $pagenow; $taxonomy = 'pa_brand'; $q_vars = &$query->query_vars; if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == 'product' && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) { $term = get_term_by('id', $q_vars[$taxonomy], $taxonomy); $q_vars[$taxonomy] = $term->slug; } }May 29, 2019 at 6:16 am #126179Hello,
We don’t know how this code is supposed to work and maintenance of the 3rd party customizations are out of our theme support scope.
Kind Regards
June 1, 2019 at 12:15 pm #126709
abParticipantHello Sir/Madam,
Please kindly help me in this issue. Millions of thanks
June 2, 2019 at 7:54 am #126767Sorry, but as we mentioned, we are not responsible for any 3rd party codes that are not related to our theme.
June 3, 2019 at 9:44 am #126891
metuzaParticipantHello there.. Please replace this action:
add_action('parse_query', 'product_tags_sorting_query');With this filter and it should work:
add_filter( 'parse_query', 'product_tags_sorting_query' );Brgds
RuneJune 3, 2019 at 2:12 pm #126959
abParticipantHi Rune,
Thank u very much for the help, I replace the string as suggested, but still when filtering it, it shows all the products. What I am missing here. Here is the code. Please help.
Thank u in advance.
add_action('restrict_manage_posts', 'product_tags_sorting'); function product_tags_sorting() { global $typenow; $taxonomy = 'product_tag'; if ( $typenow == 'product' ) { $selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : ''; $info_taxonomy = get_taxonomy($taxonomy); wp_dropdown_categories(array( 'show_option_all' => __("Show all {$info_taxonomy->label}"), 'taxonomy' => $taxonomy, 'name' => $taxonomy, 'orderby' => 'name', 'selected' => $selected, 'show_count' => true, 'hide_empty' => true, )); }; } add_action('parse_query', 'product_tags_sorting_query'); function product_tags_sorting_query($query) { global $pagenow; $taxonomy = 'product_tag'; $q_vars = &$query->query_vars; if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == 'product' && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) { $term = get_term_by('id', $q_vars[$taxonomy], $taxonomy); $q_vars[$taxonomy] = $term->slug; } }June 3, 2019 at 2:45 pm #126970
metuzaParticipantWell as from what i can see in your code you are still using “add_action” which needs to be replaced with “add_filter”.
Give this code a trie, it is tested and works:
// Filter products by brand in WP Admin
add_action( 'restrict_manage_posts', 'aws_brand_products_sorting' ); function aws_brand_products_sorting() { global $typenow; $post_type = 'product'; // change to your post type $taxonomy = 'pa_brand'; // change to your taxonomy if ($typenow == $post_type) { $selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : ''; $info_taxonomy = get_taxonomy($taxonomy); //$selected = isset($_GET[$info_taxonomy->name]) ? $_GET[$info_taxonomy->name] : ''; wp_dropdown_categories(array( 'show_option_all' => __("Show all {$info_taxonomy->label}", "aws-multi-functions"), 'taxonomy' => $taxonomy, 'name' => $taxonomy, 'orderby' => 'name', 'selected' => $selected, 'show_count' => true, 'hide_empty' => true, )); }; } add_filter( 'parse_query', 'aws_brand_products_sorting_query' ); function aws_brand_products_sorting_query( $query ) { global $pagenow; $post_type = 'product'; // change to your post type $taxonomy = 'pa_brand'; // change to your taxonomy $q_vars = &$query->query_vars; if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) { $term = get_term_by('id', $q_vars[$taxonomy], $taxonomy); $q_vars[$taxonomy] = $term->slug; } }Brgds
RuneJune 4, 2019 at 12:58 pm #127088
abParticipantHi Rune,
Thanks for the reply, I copied ur exact code and still it is showing the all the products. What I am missing, I think u r using the same theme. I think u tested it. I deactivate most of the plugins.
Any suggestion please.
June 4, 2019 at 1:12 pm #127090
metuzaParticipantHello again,
Yes i am using this code for 1-2 years and it should work with any theme. Have you checked your attribute settings that pa_brand is the correct to use, what is the slug for your brand?
And also check if it is enabled for archive ??
Brgds
RuneJune 5, 2019 at 1:17 am #127177
abParticipantHello,
WOW 🙂 You are god sent. I works like a charm. I didn’t checked the enable archive. Thank you very very much. May I ask do u maintain eCommerce website. If so I have few questions which I unable to find on the internet.
June 5, 2019 at 8:48 am #127218
metuzaParticipantHi again,
Yes i have developed wordpress/woocommerce plugins for 15 years but had the brake on for the last 2 years as i have been a little tired of developing. But just send me an email at: rune@arctic-fritid.no and we will see if i can be of any help.
Brgds
RuneSeptember 30, 2022 at 3:05 pm #409554
loraloraParticipantMetuza your code helped me a lot! Thank you !
Please tell me if your plugins are in the repository? -
AuthorPosts
- You must be logged in to create new topics. Login / Register