Add a filter dropdown for product tags in woocommerce admin product list

你离开我真会死。 提交于 2020-01-21 14:55:13

问题


I want to also add a filter tag next to the filter by category in the woocommerce products page. Below image is in ducth but I want to add another dropdown menu if possible.


回答1:


Try the following that will add filtering for product tags in admin product list:

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;
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.




回答2:


you need to use woocommerce_product_filters filter as follow:

add_filter('woocommerce_product_filters', 'tag_filter', 10, 1);

function tag_filter($output)
{
 $terms = get_terms('product_tag'); //Get all Tags
 ?> <select name="product_tag" id="product_tag_id">
  <option value="">Filter by product tags </option>
<?php
foreach ($terms as $term) { //Loop Throug tags and print the option with Tag Name
        echo '<option value=' . $term->name . '> ' . $term->name . 
'</option>';
    }
    ?>
        </select>

        <?php
}

Output :

of course you need to place this code inside your functions.php



来源:https://stackoverflow.com/questions/51964016/add-a-filter-dropdown-for-product-tags-in-woocommerce-admin-product-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!