Reputation: 1067
I have created a custom column "Cost" in Backend WooCommerce Products list panel using a hook in functions.php
file.
I want to move that column after "Price" column:
How can I achieve this?
Upvotes: 1
Views: 2593
Reputation: 253886
There is many ways to achieve this. I have chosen the simplest way to do it. You can use the code snippet below to change "Cost" column location before "price" column, in product post_type backend list:
// ADDING A CUSTOM COLUMN TITLE TO ADMIN PRODUCTS LIST AFTER "Price" COLUMN
add_filter( 'manage_edit-product_columns', 'custom_product_column',11);
function custom_product_column($columns)
{
$new_columns = array();
foreach( $columns as $key => $column ){
$new_columns[$key] = $columns[$key];
if( $key === 'price' )
$new_columns['cost'] = __( 'Cost','woocommerce');
}
return $new_columns;
}
// ADDING THE DATA FOR EACH PRODUCTS BY COLUMN (EXAMPLE)
add_action( 'manage_product_posts_custom_column' , 'custom_product_list_column_content', 10, 2 );
function custom_product_list_column_content( $column, $product_id )
{
global $post;
switch ( $column )
{
case 'cost' :
// Get and display the value for each row
echo get_post_meta( $product_id, '_cost', true );
break;
}
}
Tested and works.
Upvotes: 4