Last active
August 4, 2020 13:20
-
-
Save nocturnae/a59e16115c9019d262c5e7e10681d27f to your computer and use it in GitHub Desktop.
Add product brand column & filter to all products admin page in Woocommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add brand column to the all products admin panel | |
// Add this code to your functions.php file | |
add_filter( 'manage_edit-product_columns', 'brand_column', 20 ); | |
function brand_column( $columns_array ) { | |
// I want to display Brand column just after the product name column | |
return array_slice( $columns_array, 0, 3, true ) | |
+ array( 'brand' => 'Brand' ) | |
+ array_slice( $columns_array, 3, NULL, true ); | |
} | |
add_action( 'manage_posts_custom_column', 'populate_brands' ); | |
function populate_brands( $column_name ) { | |
if( $column_name == 'brand' ) { | |
// if you want to display multiple brands, use foreach(); | |
$x = get_the_terms( get_the_ID(), 'product_brand'); // taxonomy name | |
echo $x[0]->name; | |
} | |
} | |
//Show brand filter on all products admin page | |
add_filter( 'woocommerce_product_filters', 'filter_by_taxonomy_dashboard_products' ); | |
function filter_by_taxonomy_dashboard_products( $output ) { | |
global $wp_query; | |
$output .= wc_product_dropdown_categories( array( | |
'show_option_none' => 'Filter by product brand', | |
'taxonomy' => 'product_brand', | |
'name' => 'product_brand', | |
'selected' => isset( $wp_query->query_vars['product_brand'] ) ? $wp_query->query_vars['product_brand'] : '', | |
) ); | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment