user1990
user1990

Reputation: 1067

WooCommerce: Moving column position in admin product list

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

Answers (2)

LoicTheAztec
LoicTheAztec

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

dg4220
dg4220

Reputation: 369

Use the add_filter(manage_edit-posttype_columns, yourFunction) where posttype is your post type. Then list your columns in yourFunction the order you want. The best tutorial I've seen on this is here.

Upvotes: 0

Related Questions