Add a tax class column to WooCommerce admin products list

678 views Asked by At

This code display tax status in cloumn at admin product list page. but i need to display tax class how can i display it ?


add_filter( 'manage_edit-product_columns', 'tax_status_product_column');
function tax_status_product_column($columns){
    $new_columns = [];
    foreach( $columns as $key => $column ){
        $new_columns[$key] = $columns[$key];
        if( $key == 'is_in_stock' ) {
            $new_columns['tax_status'] = __( 'Tax status','woocommerce');
        }
    }
    return $new_columns;
}

add_action( 'manage_product_posts_custom_column', 'tax_status_product_column_content', 10, 2 );
function tax_status_product_column_content( $column, $post_id ){
    if( $column == 'tax_status' ){
        global $post, $product;

        // Excluding variable and grouped products
        if( is_a( $product, 'WC_Product' ) ) {
            $args =  array(
                'taxable'  => __( 'Taxable', 'woocommerce' ),
                'shipping' => __( 'Shipping only', 'woocommerce' ),
                'none'     => _x( 'None', 'Tax status', 'woocommerce' ),
            );

            echo $args[$product->get_tax_status()];
        }
    }
}
1

There are 1 answers

0
LoicTheAztec On BEST ANSWER

To display a product tax class column in admin product list, use the following:

add_filter( 'manage_edit-product_columns', 'tax_class_product_column');
function tax_class_product_column($columns){
    $new_columns = [];
    foreach( $columns as $key => $column ){
        $new_columns[$key] = $columns[$key];
        if( $key == 'is_in_stock' ) {
            $new_columns['tax_class'] = __( 'Tax class','woocommerce');
        }
    }
    return $new_columns;
}

add_action( 'manage_product_posts_custom_column', 'tax_class_product_column_content', 10, 2 );
function tax_class_product_column_content( $column, $post_id ){
    if( $column == 'tax_class' ){
        global $post, $product;

        // Excluding variable and grouped products
        if( is_a( $product, 'WC_Product' ) ) {
            $args = wc_get_product_tax_class_options();

            echo $args[$product->get_tax_class()];
        }
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.