Skip to content

Instantly share code, notes, and snippets.

@howbizarre
Last active December 2, 2021 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save howbizarre/895999802f1429966f09e5f98c9cd0b7 to your computer and use it in GitHub Desktop.
Save howbizarre/895999802f1429966f09e5f98c9cd0b7 to your computer and use it in GitHub Desktop.
Add custom column in WooCommerce Orders table

Add custom column in WooCommerce Orders table

Call WooCommerce filter "manage_edit-shop_order_columns", to add a new column and action "manage_shop_order_posts_custom_column", to add content in to the new column.

The code should be added in the function.php file of the child theme.

<?php
/**
* Use in function.php of the child theme.
* Call WooCommerce filter "manage_edit-shop_order_columns" to add a new column.
* Change the order of others to add new one in the right place.
*/
add_filter('manage_edit-shop_order_columns', 'add_warning_column', 10);
function add_warning_column($columns)
{
// Take current state of column order_status
$order_status = $columns['order_status'];
unset($columns['order_status']); // remove column from table
// Take current state of column order_total
$order_total = $columns['order_total'];
unset($columns['order_total']); // remove column from table
// Add columns in the correct order
$columns['order_status'] = $order_status;
$columns['order_error'] = "Status Info";
$columns['order_total'] = $order_total;
return $columns;
}
/**
* Call WooCommerce action "manage_shop_order_posts_custom_column" to add content in to the new column.
* FYI: The content is exemplary and should be rewritten with the business logic that will be used.
*/
add_action('manage_shop_order_posts_custom_column', 'update_order_status_if_error');
function update_order_status_if_error($colname)
{
global $the_order;
// check if the new column is on the way
if ($colname === 'order_error') {
$order_status = $the_order->get_status();
// add content to the column
if ($order_status === "failed") {
echo '<mark style="background:red;color:white" class="order-status tips"><span>Error</span></mark>';
} else {
echo '<mark style="background:green;color:white" class="order-status tips"><span>OK</span></mark>';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment