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.
PHP Snippet: Add Column to Products Table @ WooCommerce Dashboard
/**
* @snippet New Products Table Column @ WooCommerce Admin
* @how-to Get CustomizeWoo.com FREE
* @author Rodolfo Melogli
* @compatible WooCommerce 5
* @community https://businessbloomer.com/club/
*/
add_filter( 'manage_edit-product_columns', 'bbloomer_admin_products_visibility_column', 9999 );
function bbloomer_admin_products_visibility_column( $columns ){
$columns['visibility'] = 'Visibility';
return $columns;
}
add_action( 'manage_product_posts_custom_column', 'bbloomer_admin_products_visibility_column_content', 10, 2 );
function bbloomer_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 bbloomer_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', 'bbloomer_admin_products_visibility_column_sortable' );
function bbloomer_admin_products_visibility_column_sortable( $columns ){
$columns['visibility'] = 'visibility';
return $columns;
}
Your tutorials, etc. are brilliant, thank you!
In terms of this one, how do we add an ACF field based column please?
My listings are books and I’ve added a field group with an ACF custom field ‘isbn’ (text field).
I would like to be able to display these and sort by these.
Thank you in advance!
Hello Peter, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi Rodolfo, I tried to do this to display the shipping class of the product but got the following error:
Warning: Illegal string offset ‘shipping_class’ in /nas/content/live/mysite/wp-content/themes/astra-child/functions.php on line 89
This was the code I adapted:
Can you tell me what I have done wrong?
Of course. In this line, the name of the function is wrong:
Hi
Sorting is not working unfortunately.
Hi Hakan, what’s not working exactly? After you click it sorts them randomly? What type of data did you display?
Thanks
If i wanted a colum with the product ID number??
What line should i change, and to what??
Just this one:
to:
A little late to the game and a bit of a novice. I’ve added the function and the column is there but there is no CSS at all and I’m a little unsure how to add that for the Admin area.
Can you advise on how to place this CSS? As shown in your picture is exactly what I desire so if you could share and where to put it I would appreciate it.
Hey Jeff thanks for your comment! What is the problem / are you trying to achieve? CSS is probably not needed. Let me know
Thanks, nice of you to reply.
So this adds the Visibility column to the far right in a very thin line (one letter wide with word stretching down) and not in a “normal” column like the other column fields.
Separate issue but the sort does not seem to work as I would expect. I would think “hidden” would come to the top but it’s mixed in the results with seemingly no sort in play.
Even “broken” as it is this is currently is still helpful.
I started this because it appears I need to also set “private” so google, for example, search results don’t take people to a deactivated (for a reason) product.
Thanks Jeff! For the column width, there is not much you can do apart from removing some other column so that this one fits in. Applies to all WordPress tables. Go to “Screen Options” (top right) and toggle columns. This will show properly as soon as you hide one default column.
For the other query, unfortunately “visibility” is now a custom taxonomy, and I found it very difficult trying to sort by it despite my attempts. The ideal would be to save the visibility value into a custom field, so that you can simply sort by it.
Fair enough and thanks. Didn’t even occur regarding the screen options and now I can at least see the column. Maybe if time frees up I’ll explore making it function the way I would like but this was useful and thanks for your time and for providing this for everyone.
You’re welcome!
Hi, how would we do this with a plugins meta field? We have a plugin that adds a UPC code to products and the meta field is _rank_math_gtin_code
Hey Stavros! You can use get_post_meta() WordPress function to retrieve that custom field and then echo it. Hope this helps!
This snippet does not appear to work on WooCommerce 4.1.0 and WordPress 5.4.1.
Tried with a different theme and no plugins active apart Woo?
Sir also show us how to edit to desired custom field such as “yith cost of goods” etc
Sam, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
As always, great information. Very helpful.
How do you set the column width?
Not sure you can do that David!
Column width can be set with admin side CSS that you inject in your functions.php as follows: