WooCommerce:向产品管理区域添加自定义过滤器 [英] WooCommerce: Adding a custom filter to the Product Admin area

查看:35
本文介绍了WooCommerce:向产品管理区域添加自定义过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近使用以下过滤器和操作组合在 WooCommerce 管理中的产品管理"部分添加了一列:manage_product_posts_columnsmanage_product_posts_custom_column.

I've recently added a column to my Product Admin section in WooCommerce admin using the following filter and action combination: manage_product_posts_columns, manage_product_posts_custom_column.

我的问题是,是否有一个钩子不允许我为此列添加过滤器?我找不到,但我确定有可能吗?

My question is, is there a hook that will allow me no add a filter for this column? I can't find one but I'm sure it is possible?

谢谢

推荐答案

你的问题很模糊,所以我假设你的自定义列显示每个产品的元信息.

Your question is very vague, so I will assume your custom columns display meta information for each product.

首先,您需要使用 restrict_manage_posts 将您自己的字段添加到产品"管理页面顶部的过滤器"区域的 WordPress 操作:

Firstly, you will need to use the restrict_manage_posts WordPress action to add your own fields to the 'Filter' area at the top of the 'products' admin page:

function my_custom_product_filters( $post_type ) {

$value1 = '';
$value2 = '';

// Check if filter has been applied already so we can adjust the input element accordingly

if( isset( $_GET['my_filter'] ) ) {

  switch( $_GET['my_filter'] ) {

    // We will add the "selected" attribute to the appropriate <option> if the filter has already been applied
    case 'value1':
      $value1 = ' selected';
      break;

    case 'value2':
      $value2 = ' selected';
      break;

  }

}

// Check this is the products screen
if( $post_type == 'product' ) {

  // Add your filter input here. Make sure the input name matches the $_GET value you are checking above.
  echo '<select name="my_filter">';

    echo '<option value>Show all value types</option>';
    echo '<option value="value1"' . $value1 . '>First value</option>';
    echo '<option value="value2"' . $value2 . '>Second value</option>';

  echo '</select>';

}

}

add_action( 'restrict_manage_posts', 'my_custom_product_filters' );

注意:从 WP4.4 开始,此操作提供 $post_type 作为参数,因此您可以轻松识别正在查看的帖子类型.在 WP4.4 之前,您需要使用 $typenow 全局或 get_current_screen() 函数来检查这个.这个要点提供了一个很好的例子.

Note: As of WP4.4, this action provides $post_type as a parameter, so you can easily identify which post type is being viewed. Prior to WP4.4, you needed to use the $typenow global or get_current_screen() function to check this. This Gist offers a good example.

为了使过滤器真正起作用,我们需要在加载产品"管理页面时向 WP_Query 添加一些额外的参数.为此,我们需要使用 pre_get_posts WordPress像这样的动作:

To make the filters actually work, we need to add some extra parameters to the WP_Query when the 'products' admin page is loaded. To do this, we need to use the pre_get_posts WordPress action like so:

function apply_my_custom_product_filters( $query ) {

global $pagenow;

// Ensure it is an edit.php admin page, the filter exists and has a value, and that it's the products page
if ( $query->is_admin && $pagenow == 'edit.php' && isset( $_GET['my_filter'] ) && $_GET['my_filter'] != '' && $_GET['post_type'] == 'product' ) {

  // Create meta query array and add to WP_Query
  $meta_key_query = array(
    array(
      'key'     => '_my_meta_value',
      'value'   => esc_attr( $_GET['my_filter'] ),
    )
  );
  $query->set( 'meta_query', $meta_key_query );

}

}

add_action( 'pre_get_posts', 'apply_my_custom_product_filters' );

这是自定义过滤器的基础知识,它适用于任何帖子类型(包括 WooCommerce shop_orders).您还可以为元查询设置比较"值(以及任何其他可用选项),或者根据需要调整 WP_Query 的不同方面.

That is the basics of custom filters, and it works on any post type (including WooCommerce shop_orders). You can also set the "compare" value for the meta query (and any other options available), or adjust different aspects of the WP_Query if you wish.

这篇关于WooCommerce:向产品管理区域添加自定义过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆