【问题标题】:Add a filter dropdown for product tags in woocommerce admin product list在 woocommerce 管理产品列表中为产品标签添加过滤器下拉列表
【发布时间】:2019-05-11 03:17:54
【问题描述】:

我还想在 woocommerce 产品页面的按类别过滤器旁边添加一个过滤器标签。下图是在导管中,但如果可能的话,我想添加另一个下拉菜单。

【问题讨论】:

  • 那么你还需要过滤带有标签的产品吗?
  • 是的,所以我有一个带有我可以选择的标签的下拉菜单。仅在 woocmmerce 管理菜单中
  • 你想在用户端进行标签过滤吗?就像产品过滤器通常位于的侧边栏一样?
  • 不仅是 wp-admin 方面,我已经添加了图片你知道答案吗?
  • 这个答案可能会对您有所帮助。 wordpress.stackexchange.com/questions/112909/…

标签: php wordpress woocommerce product custom-taxonomy


【解决方案1】:

尝试以下操作,在管理产品列表中添加产品标签过滤:

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

代码在您的活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。

【讨论】:

  • 我可以在商店页面上做同样的事情吗?
【解决方案2】:

你需要使用woocommerce_product_filters过滤器如下:

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
}

输出:

当然你需要把这段代码放在你的functions.php中

【讨论】:

    猜你喜欢
    • 2019-06-28
    • 2022-08-05
    • 2018-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 2020-06-18
    • 1970-01-01
    相关资源
    最近更新 更多