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?

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #126165

    ab
    Participant

    Hi,

    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.
    #126167

    ab
    Participant

    Please 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;
        }
    }
    #126179

    Artem Temos
    Keymaster

    Hello,

    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

    #126709

    ab
    Participant

    Hello Sir/Madam,

    Please kindly help me in this issue. Millions of thanks

    #126767

    Artem Temos
    Keymaster

    Sorry, but as we mentioned, we are not responsible for any 3rd party codes that are not related to our theme.

    #126891

    metuza
    Participant

    Hello 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
    Rune

    #126959

    ab
    Participant

    Hi 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;
        }
    }
    
    #126970

    metuza
    Participant

    Well 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
    Rune

    #127088

    ab
    Participant

    Hi 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.

    #127090

    metuza
    Participant

    Hello 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
    Rune

    #127177

    ab
    Participant

    Hello,

    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.

    #127218

    metuza
    Participant

    Hi 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
    Rune

    #409554

    loralora
    Participant

    Metuza your code helped me a lot! Thank you !
    Please tell me if your plugins are in the repository?

Viewing 13 posts - 1 through 13 (of 13 total)