将税类列添加到WooCommerce管理产品列表

时间:2020-10-20 22:00:00

标签: php wordpress woocommerce product tax

此代码在admin产品列表页面的cloumn中显示税收状态。但是我需要显示税收类别,如何显示它?


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 个答案:

答案 0 :(得分:1)

要在管理产品列表中显示产品税类列,请使用以下内容:

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()];
        }
    }
}

代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。