Home » WooCommerce: Add Product Table Columns @ Admin

WooCommerce: Add Product Table Columns @ Admin

by adminkys

The default WooCommerce Dashboard Products page (/wp-admin/edit.php?post_type=product page) shows the list of products in a table. Default fields are: Image, SKU, Stock, Price, Categories, Tags, Featured and Date.

Sometimes, these columns are not enough and you need more. For example, you might want to quickly take a look at a product custom field, such as “visibility” (whether the product is hidden or not).

So, here’s the snippet for that. Of course, you can adapt it to show your own custom field, an ACF field or whatever product-related information you require.

Here’s a new WooCommerce Products Table column – in this case it’s the products’s “visibility” status

PHP Snippet: Add Column to Products Table @ WooCommerce Dashboard

/**
 * @snippet       New Products Table Column @ WooCommerce Admin
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 5
 * @donate $9     https://www.tutoraspire.com
 */

add_filter( 'manage_edit-product_columns', 'tutoraspire_admin_products_visibility_column', 9999 );

function tutoraspire_admin_products_visibility_column( $columns ){
$columns['visibility'] = 'Visibility';
return $columns;
}

add_action( 'manage_product_posts_custom_column', 'tutoraspire_admin_products_visibility_column_content', 10, 2 );

function tutoraspire_admin_products_visibility_column_content( $column, $product_id ){
    if ( $column == 'visibility' ) {
        $product = wc_get_product( $product_id );
echo $product->get_catalog_visibility();
    }
}

Bonus PHP Snippet 1: Place New Column In A Specific Position

Inside the tutoraspire_admin_products_visibility_column function, write this instead (you will be placing the column “Visibility” in position “3”, so edit all occurrences of “3”to define another position):

return array_slice( $columns, 0, 3, true ) + array( 'visibility' => 'Visibility' ) + array_slice( $columns, 3, count( $columns ) - 3, true );

Bonus PHP Snippet 2: Make New Column Sortable

Simply add this snippet to make your column sortable as well.

Please note, this won’t work specifically for the “visibility” field, as it’s now considered and stored as a taxonomy. You can only sort by custom fields.

add_filter( 'manage_edit-product_sortable_columns', 'tutoraspire_admin_products_visibility_column_sortable' );

function tutoraspire_admin_products_visibility_column_sortable( $columns ){
$columns['visibility'] = 'visibility';
return $columns;
}

You may also like