Liam Fell
Liam Fell

Reputation: 1320

WooCommerce: Adding a custom filter to the Product Admin area

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?

Thanks

Upvotes: 1

Views: 14088

Answers (1)

Cameron
Cameron

Reputation: 91

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


Adding the Filter(s)

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

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.


Applying the Filter(s)

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

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.

Upvotes: 9

Related Questions