本文介绍了在 Woocommerce Admin 产品列表中的产品类别过滤器之后添加自定义分类过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 woocommerce 店面子主题中,我添加了几个分类法.现在我想为这些自定义分类添加一些类别过滤器.我使用此代码添加了这样一个过滤器(来源:Rodolfo Melogli)

In my woocommerce storefront child theme, I have added several taxonomies. Now I would like to add a few category filters for those custom taxonomies. I have added such a filter using this code (credit: Rodolfo Melogli)

add_filter( 'woocommerce_product_filters', 'admin_filter_products_by_din' );
function admin_filter_products_by_din( $output ) {

  global $wp_query;

  $output .= wc_product_dropdown_categories( array(
    'show_option_all' => 'All DIN/ISO/ANSI',
    'taxonomy' => 'din-iso-ansi',
    'name' => 'din-iso-ansi',
    'order' => 'ASC',
    'tab_index' => '2',
    'selected' => isset( $wp_query->query_vars['din-iso-ansi'] ) ? $wp_query->query_vars['din-iso-ansi'] : '',
  ) );

  return $output;
}

显示新类别过滤器,但现在我希望我的新分类过滤器 (DIN/ISO/ANSI) 位于产品类别过滤器之后.

The new category filter displays, but now I want the placement of my new taxonomy filter (DIN/ISO/ANSI) to go after the Product Categories filter.

产品管理员:

推荐答案

我在 LoicTheAztec 的很多帮助下解决了这个问题,基本上使用了他的大部分代码,但似乎我们无法替代 wp_dropdown_categories wc_product_dropdown_categories 如此轻松.在查看了 wc_product_dropdown_categories 的函数构成后,我实现了另一种方法,以避免通过一点 php 的方式让该函数回显结果.

I figured this out with a lot of help from LoicTheAztec, essentially using most of his code, but it appears that we cannot substitute wp_dropdown_categories for wc_product_dropdown_categories so easily. After reviewing the function make-up of wc_product_dropdown_categories, I implemented another way to avoid having this function echo out the results by way of a little php.

add_filter( 'woocommerce_product_filters', 'admin_filter_products_by_din' );
function admin_filter_products_by_din( $output ) {

    global $wp_query;

    $taxonomy  = 'din-iso-ansi';
    $selected      = isset( $wp_query->query_vars[$taxonomy] ) ? $wp_query->query_vars[$taxonomy] : '';
    $info_taxonomy = get_taxonomy($taxonomy);

    ob_start(); // buffer the result of wc_product_dropdown_categories silently
    wc_product_dropdown_categories( array(
        'show_option_none' => __("Select a {$info_taxonomy->label}"), // changed
        'taxonomy'         => $taxonomy,
        'name'             => $taxonomy,
        //'echo'             => false, // <== Needed for in filter hook
        'tab_index'        => '2',
        'selected'         => $selected,
        'show_count'       => true,
        'hide_empty'       => true,
    ));
    $custom_dropdown = ob_get_clean();


    $before = '<select name="product_type"'; //

    $output = str_replace( $before, $custom_dropdown . $before, $output );

    return $output;
} 

这篇关于在 Woocommerce Admin 产品列表中的产品类别过滤器之后添加自定义分类过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 15:04