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

问题描述 投票:0回答:1

此代码在管理产品列表页面的列中显示税务状态。但我需要显示税级,如何显示?


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()];
        }
    }
}
php wordpress woocommerce product
1个回答
2
投票

要在管理产品列表中显示产品税级列,请使用以下命令:

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 文件中。已测试并有效。

© www.soinside.com 2019 - 2024. All rights reserved.