In WooCommerce, checking the purchased items for each order can be cumbersome as it often requires opening each individual order or downloading reports. However, there’s a faster solution.
This code snippet restores a useful feature that was once part of WooCommerce by adding a new column to the WooCommerce orders list (Woocommerce>Orders). The column displays the names of purchased products for each order, improving order management by giving you a quick overview of the items bought, without the need to open each order.
This code should be added to functions.php of your child theme, or inserted through the Code Snippets plugin.
// Add Purchased Products Column to WooCommerce Orders
add_filter('manage_edit-shop_order_columns', 'custom_shop_order_column', 20);
function custom_shop_order_column($columns) {
$new_columns = array();
foreach ($columns as $column_name => $column_info) {
$new_columns[$column_name] = $column_info;
if ('order_status' === $column_name) {
$new_columns['order_products'] = __('Product', 'woocommerce');
}
}
return $new_columns;
}
add_action('manage_shop_order_posts_custom_column', 'custom_shop_order_column_data');
function custom_shop_order_column_data($column) {
global $post;
if ('order_products' === $column) {
$order = wc_get_order($post->ID);
$items = $order->get_items();
$product_names = array();
foreach ($items as $item) {
$product_names[] = $item->get_name();
}
echo !empty($product_names) ? implode(', ', $product_names) : __('Nothing found', 'woocommerce');
}
}
Leave a Reply