-1

在我的 woocommerce 店面子主题中,我添加了几个分类法。现在我想为这些自定义分类添加一些类别过滤器。我使用此代码添加了这样一个过滤器(来源: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) 放置在产品类别过滤器之后。

产品管理员:

产品管理员

4

2 回答 2

0

重要笔记:

  • wc_product_dropdown_categories()默认情况下会回显函数输出,并且在始终返回所有过滤数据的过滤器挂钩中不方便,因此我们将使用'echo'设置为false.
  • wc_product_dropdown_categories()对于像您这样的自定义分类法,函数实际上使用了wp_dropdown_categories()更方便的方法。

为了测试您的代码,我使用product_tag了 Woocommerce 自定义分类法来确保它可以正常工作。

以下代码会将您的自定义过滤器下拉列表放在产品类别过滤器之后:

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

    $custom_dropdown = wp_dropdown_categories(array(
        'show_option_none' => __("Select a {$info_taxonomy->label}"), // changed
        'taxonomy'         => $taxonomy,
        'name'             => $taxonomy,
        'order'            => 'ASC',
        'echo'             => false, // <== Needed in a filter hook
        'tab_index'        => '2',
        'selected'         => $selected,
        'show_count'       => true,
        'hide_empty'       => true,
        'value_field'      => 'slug',
    ));

    $after = '<select name="product_type"'; // The start of the html output of product type filter dropdown.

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

    return $output;
}

代码位于您的活动子主题(或活动主题)的 function.php 文件中。测试和工作。

在此处输入图像描述

于 2018-12-12T23:46:51.910 回答
0

在 LoicTheAztec 的大量帮助下,我发现了这一点,基本上使用了他的大部分代码,但似乎我们不能轻易地将wp_dropdown_categories 替换为wc_product_dropdown_categories。在查看了 wc_product_dropdown_categories 的函数组成之后,我实现了另一种方法来避免这个函数通过一点 php.ini 来回显结果。

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;
} 
于 2018-12-14T16:40:56.510 回答