如何在管理页面的产品列表上显示产品“税务状态”(即应税、仅送货、无)?
我一直尝试在产品列表上将产品显示为“有货/缺货”和“应税/不应税”,以便我可以轻松确定产品的状态并在必要时进行更改。
我想出了如何在产品列表上显示库存状态,但是,当我尝试使用“税务状态”进行相同操作时,它不起作用。我通过将以下代码添加到functions.php来显示库存状态。我怎样才能显示“税务状态”?
add_filter( 'manage_edit-product_columns', 'product_column_arrange' );
function product_column_arrange( $product_columns ) {
return array(
...
'is_in_stock' => 'Stock',
...
);
}
以下将在管理产品列表中显示产品税收状态的自定义列:
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()];
}
}
}
代码位于活动子主题(或活动主题)的 function.php 文件中。已测试并可以使用。
刚刚扩展了 @LoicTheAztec 代码以使该列可排序:
// Add new column to All Products page list
add_filter( 'manage_edit-product_columns', function($columns) {
$new_columns = [];
foreach( $columns as $key => $column ) {
$new_columns[$key] = $columns[$key];
if ( 'price' === $key ) {
$new_columns['tax_status'] = __( 'Tax status', 'woocommerce' );
}
}
return $new_columns;
} );
// Populate the Tax Status column with data
add_action( 'manage_product_posts_custom_column', function( $column, $post_id ) {
if( 'tax_status' === $column ) {
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()];
}
}
}, 10, 2 );
// Make the Tax Status column sortable
add_filter( 'manage_edit-product_sortable_columns', function( $columns ) {
$columns['tax_status'] = 'tax_status';
return $columns;
} );
// Custom sorting for the Tax Status column
add_filter( 'request', function( $vars ) {
if ( isset( $vars['orderby'] ) && 'tax_status' === $vars['orderby'] ) {
$vars = array_merge( $vars, array(
'meta_key' => '_tax_status',
'orderby' => 'meta_value'
) );
}
return $vars;
} );