Add Custom Columns to Admin Products List in Woocommerce 3

Add custom columns to admin products list in WooCommerce 3

Here is the way to do it with that 2 custom functions hooked. The first one create the column with the title, the second one populate the column with the products data. But you will need to set in that second function, the correct corresponding meta_key to get the data.

Here is that code:

// ADDING A CUSTOM COLUMN TITLE TO ADMIN PRODUCTS LIST
add_filter( 'manage_edit-product_columns', 'custom_product_column',11);
function custom_product_column($columns)
{
//add columns
$columns['delivery'] = __( 'Delivery time','woocommerce'); // title
return $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;

// HERE get the data from your custom field (set the correct meta key below)
$delivery_time = get_post_meta( $product_id, '_delivery_time', true );

switch ( $column )
{
case 'delivery' :
echo $delivery_time; // display the data
break;
}
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works.


How to get the correct meta_key slug:

To find the correct meta_key slug corresponding to the "delivery time", you should need to make search in your database using PhpMyAdmin. You will have to search for delivery term in wp_postmeta table this way:

Sample Image

Then you will get this kind of results (here there is just 1 line with a fake slug):

Sample Image

So now you should be able to get the correct slug name (like this fake "_delivery_date" one)


Related answer (for orders): Add custom columns to admin orders list in WooCommerce backend

Add custom column product visibility to admin product list in Woocommerce 3

There are some errors and mistakes in your code. Also since Woocommerce 3 product visibility is handled by Woocommerce custom taxonomy 'product_visibility'. Try the following instead:

// Add a new column to Admin products list with a custom order
add_filter( 'manage_edit-product_columns', 'visibility_product_column', 10);
function visibility_product_column($columns){
$new_columns = [];
foreach( $columns as $key => $column ){
$new_columns[$key] = $columns[$key];
if( $key == 'price' ) { // Or use: if( $key == 'featured' ) {
$new_columns['visibility'] = __( 'Visibility','woocommerce');
}
}
return $new_columns;
}

// Add content to new column raows in Admin products list
add_action( 'manage_product_posts_custom_column', 'visibility_product_column_content', 10, 2 );
function visibility_product_column_content( $column, $product_id ){
global $post;

if( $column =='visibility' ){
if( has_term( 'exclude-from-catalog', 'product_visibility', $product_id ) )
echo '<em style="color:grey;">' . __("No") . '</em>';
else
echo '<span style="color:green;">' . __("Yes") . '</span>';
}
}

Code goes in function.php file of your active child theme (active theme). Tested and works.

Sample Image

WooCommerce: Add new column in products list view and make it sortable

// Add product new column in administration
add_filter( 'manage_edit-product_columns', 'woo_product_weight_column', 20 );
function woo_product_weight_column( $columns ) {

$columns['total_weight'] = esc_html__( 'Weight', 'woocommerce' );
return $columns;

}
// Populate weight column
add_action( 'manage_product_posts_custom_column', 'woo_product_weight_column_data', 2 );
function woo_product_weight_column_data( $column ) {
global $post;

if ( $column == 'total_weight' ) {
$product = wc_get_product($post->ID);
$weight = $product->get_weight();
if ( $weight > 0 )
print $weight . ' ' . esc_attr( get_option('woocommerce_weight_unit' ) );
else print 'N/A';
}
}

// Add CSS for column width

add_action('admin_head', 'my_column_width');

function my_column_width() {
echo '<style type="text/css">';
echo 'table.wp-list-table .column-total_weight { width: 46px; text-align: left!important;padding: 5px;}';
echo '</style>';
}

// make column sortable

function my_set_sortable_columns( $columns )
{
$columns['total_weight'] = 'total_weight';
return $columns;
}

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

// Sorting function

function my_sort_custom_column_query( $query )
{
$orderby = $query->get( 'orderby' );

if ( 'total_weight' == $orderby ) {

$meta_query = array(
'relation' => 'OR',
array(
'key' => '_weight',
'compare' => '>', // see note above
),
array(
'key' => '_weight',
),
);

$query->set( 'meta_query', $meta_query );
$query->set( 'orderby', 'meta_value' );
}
}

add_action( 'pre_get_posts', 'my_sort_custom_column_query' );

Tested OK with WooCommerce 3.5.7

How to add / remove columns in Woocommerce admin product list

The filter manage_edit-{post_type}_columns is only used to actually add the column. To control what is displayed in the column for each post (product), you can use the manage_{post_type}_posts_custom_column action. This action is called for each custom column for every post, and it passes two arguments: $column and $postid.

Using this action is quite easy, you can find an example to display the custom field "offercode" below:

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

function wpso23858236_product_column_offercode( $column, $postid ) {
if ( $column == 'offercode' ) {
echo get_post_meta( $postid, 'offercode', true );
}
}

You could also use a plugin to control this behaviour, such as Admin Columns.

Add multiple custom columns (notes and VAT) on WooCommerce admin orders list

To add 2 columns versus 1 you can actually apply the same as you already did for adding 1 column

Adding the VAT number to the order can be done in different ways, from your question I understand that this is a custom checkout field with the meta key: billing_piva

Note: the use of add_action( 'wp_enqueue_scripts', 'mini_enqueue_scripts' ); is not necessary

So you get:

// Display on order admin list (header)
function filter_manage_edit_shop_order_columns( $columns ) {
// Add columns
$columns['order_notes'] = __( 'Customer note', 'woocommerce' );
$columns['order_vat'] = __( 'VAT number', 'woocommerce' );

return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );

// Display on order admin list (populate the column)
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {
// Get order
$order = wc_get_order( $post_id );

// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
// Compare
switch ( $column ) {
case 'order_notes':
// Get customer note
$note = $order->get_customer_note();

// NOT empty
if ( ! empty( $note ) ) {
echo '<span class="note-on tips" data-tip="' . wc_sanitize_tooltip( $note ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
} else {
echo '<span class="na">–</span>';
}

break;
case 'order_vat':
// Get VAT (if necessary, adjust to the correct meta key)
$vat_number = $order->get_meta( 'billing_piva' );

// NOT empty
if ( ! empty( $vat_number ) ) {
// Output
$output = '<span>' . sprintf( __( 'VAT Number: %s', 'woocommerce' ), $vat_number ) . '</span>';

// Get CID number
$cid_number = $order->get_meta( 'billing_cid' );

// NOT empty
if ( ! empty ( $cid_number ) ) {
// Concatenation
$output .= '<br><span>' . sprintf( __( 'CID Number: %s', 'woocommerce' ), $cid_number ) . '</span>';
}

// Print
echo $output;
} else {
echo '<span class="na">–</span>';
}
}
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );

Add a custom Sale Price column to admin products list in Woocommerce

There is some mistakes in your code… Try the following instead:

add_filter( 'manage_edit-product_columns', 'onsale_product_column', 10);
function onsale_product_column($columns){
$new_columns = [];
foreach( $columns as $key => $column ){
$new_columns[$key] = $columns[$key];
if( $key == 'product_cat' ) {
$new_columns['onsale'] = __( 'Sale Price','woocommerce');
}
}
return $new_columns;
}

add_action( 'manage_product_posts_custom_column', 'onsale_product_column_content', 10, 2 );
function onsale_product_column_content( $column, $post_id ){
if( $column == 'onsale' ){
global $post, $product;

// Excluding variable and grouped products
if( is_a( $product, 'WC_Product' ) && ! $product->is_type('grouped') &&
! $product->is_type('variable') && $product->is_on_sale() ) {
echo strip_tags( wc_price( $product->get_sale_price() ) );
}
}
}

Code goes in function.php file of your active child theme (or active theme). Tested and work.

Add multiple custom columns to WooCommerce My account orders table

The woocommerce_account_orders_columns filter hook allows us to add 1 or more columns

// Add new column(s) to the "My Orders" table in the account.
function filter_woocommerce_account_orders_columns( $columns ) {
$columns['custom-column'] = __( 'New Column 1', 'woocommerce' );
$columns['custom-column2'] = __( 'New Column 2', 'woocommerce' );

return $columns;
}
add_filter( 'woocommerce_account_orders_columns', 'filter_woocommerce_account_orders_columns', 10, 1 );

However, adding content per column is done via the woocommerce_my_account_my_orders_column_{$column_id} action hook.

So {$column_id} need to be replaced by the column key slug (custom-column or custom-column2) in this particular case.
It is therefore not necessary to determine the correct column in the callback function via an if condition

So you get:

// Adds data to the custom column in "My Account > Orders"
function filter_woocommerce_my_account_my_orders_column_custom_column( $order ) {
echo 'New Column 1';
}
add_action( 'woocommerce_my_account_my_orders_column_custom-column', 'filter_woocommerce_my_account_my_orders_column_custom_column', 10, 1 );

// Adds data to the custom column in "My Account > Orders"
function filter_woocommerce_my_account_my_orders_column_custom_column2( $order ) {
echo 'New Column 2';
}
add_action( 'woocommerce_my_account_my_orders_column_custom-column2', 'filter_woocommerce_my_account_my_orders_column_custom_column2', 10, 1 );

How to add ACF field to custom column on WooCommerce admin orders list

An order generally consists of several products, therefore you cannot use $product_id directly, but you have to loop through the order items.

So you get:

/**
* Add columns
*/
function filter_manage_edit_shop_order_columns( $columns ) {
$reordered_columns = array();

foreach ( $columns as $key => $column ) {
$reordered_columns[$key] = $column;

if ( $key == 'order_status' ) {
$reordered_columns['my-column'] = __( 'Location','theme_domain' );
}
}

return $reordered_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );

/**
* Populate columns
*/
function filter_manage_shop_order_posts_custom_column( $column, $post_id ) {
// Compare
if ( $column == 'my-column' ) {
// Get order
$order = wc_get_order( $post_id );

// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
// Get items
$items = $order->get_items();

// Loop through
foreach ( $items as $key => $item ) {
// Product ID
$product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();

// Get field
$address = get_field( 'location', $product_id );

// Output
echo ($address) ? '<div>Address: ' . $address . '</div>' : '<div>Address: No address found!</div>';
}
}
}
}
add_filter( 'manage_shop_order_posts_custom_column', 'filter_manage_shop_order_posts_custom_column', 10, 2 );

Add a custom column to order items and make it sortable in WooCommerce admin order details page

  • Note that I've added some CSS classes, to make it sortable
  • Also notice that I'm using an array of strings for the output, and displaying them randomly. Replace this with your own code. This is because my products do not contain the specific terms

So to make your custom column added to admin order item sortable, you can use:

// Add header
function action_woocommerce_admin_order_item_headers( $order ) {
// Set the column name
$column_name = __( 'Packing Weight', 'woocommerce' );

// Display the column name
echo '<th class="line_packing_weight sortable" data-sort="string-ins">' . $column_name . '</th>';
}
add_action( 'woocommerce_admin_order_item_headers', 'action_woocommerce_admin_order_item_headers', 10, 1 );

//Add content
function action_woocommerce_admin_order_item_values( $product = null, $item, $item_id ) {
// WC_Order_Refund OR WC_Order_item
if ( $item->get_type() == 'shop_order_refund' ) {
$item = new WC_Order_Refund( $item_id );
} else {
$item = new WC_Order_Item_Product( $item_id );

// Only for "line_item" items type, to avoid errors
if ( ! $item->is_type( 'line_item' ) ) return;
}

// Replace this part with your own code
$some_strings = array( 'Soft', 'Very soft', 'Hard' );

// Replace this part with your own code
$value = $some_strings[array_rand( $some_strings )];

if ( $value ) {
echo '<td class="line_packing_weight" data-sort-value="' . $value . '">' . $value . '</td>';
}
}
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );

Add new column to WooCommerce admin products list with discount percentage on sale products

UPDATE 06/21: now also works for variable products.

It is not necessary to get the postmeta data via get_post_meta because you can access the product object via the $postid.

Once you have the product object, you have access to all kinds of product information.

  • WooCommerce: Get Product Info (ID, SKU, $) From $product Object

So you get:

// Column header
function filter_manage_edit_product_columns( $columns ) {
// Add column
$columns['discount'] = __( 'Discount', 'woocommerce' );

return $columns;
}
add_filter( 'manage_edit-product_columns', 'filter_manage_edit_product_columns', 10, 1 );

// Column content
function action_manage_product_posts_custom_column( $column, $postid ) {
// Compare
if ( $column == 'discount' ) {
// Get product object
$product = wc_get_product( $postid );

// Is a WC product
if ( is_a( $product, 'WC_Product' ) ) {
// Product is on sale
if ( $product->is_on_sale() ) {
// Output
echo '<ul>';

// Simple products
if ( $product->is_type( 'simple' ) ) {
// Get regular price
$regular_price = $product->get_regular_price();

// Get sale price
$sale_price = $product->get_sale_price();

// Calculate discount percentage
$discount_percentage = ( ( $sale_price - $regular_price ) / $regular_price ) * 100;

// Output
echo '<li>' . abs( number_format( $discount_percentage, 2, '.', '') ) . '%' . '</li>';
// Variable products
} elseif ( $product->is_type( 'variable' ) ) {
foreach( $product->get_visible_children() as $variation_id ) {
// Get product
$variation = wc_get_product( $variation_id );

// Get regular price
$regular_price = $variation->get_regular_price();

// Get sale price
$sale_price = $variation->get_sale_price();

// NOT empty
if ( ! empty ( $sale_price ) ) {
// Get name
$name = $variation->get_name();

// Calculate discount percentage
$discount_percentage = ( ( $sale_price - $regular_price ) / $regular_price ) * 100;

// Output
echo '<li>' . $name . '</li>';
echo '<li>' . abs( number_format( $discount_percentage, 2, '.', '') ) . '%' . '</li>';
}
}
}

// Output
echo '</ul>';
}
}
}
}
add_action( 'manage_product_posts_custom_column', 'action_manage_product_posts_custom_column', 10, 2 );


Related Topics



Leave a reply



Submit