Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prikhi/5f497142ff68be3c13073bc641e85522 to your computer and use it in GitHub Desktop.
Save prikhi/5f497142ff68be3c13073bc641e85522 to your computer and use it in GitHub Desktop.
[WooCommerce 3.0+] Re-instate the "Purchased items" column on orders page
<?php
/** Add a Purchased Column w/ Item Quantity, SKU, Name, & Meta to the Admin Orders Table **/
class ThemeWooCommerce
{
/* Add a `Purchased` Column to the Admin Orders Table */
public static function add_purchased_column_header($columns) {
$new_columns = array();
foreach ($columns as $key => $title) {
if ($key === 'billing_address') {
$new_columns['order_items'] = __('Purchased', 'woocommerce');
}
$new_columns[$key] = $title;
}
return $new_columns;
}
/* Render the `Purchased` Column in the Admin Orders Table */
public static function render_purchased_column($column) {
global $the_order;
if ($column === 'order_items') {
$item_count = $the_order->get_item_count();
$item_count_text =
apply_filters(
'woocommerce_admin_order_item_count',
sprintf(_n('%d item', '%d items', $item_count, 'woocommerce'), $item_count),
$the_order
);
echo "<a href='#' class='show_order_items'>{$item_count_text}</a>";
$order_items = $the_order->get_items();
if (sizeof($order_items) > 0) {
echo "<table class='order_items' cellspacing='0'>";
foreach ($order_items as $item) {
$product = apply_filters(
'woocommerce_order_item_product', $item->get_product(), $item);
$item_meta = new WC_Order_Item_Meta($item, $product);
$item_meta_html = $item_meta->display(true, true);
$item_class = apply_filters(
'woocommerce_admin_order_item_class', '', $item, $the_order);
$item_name = apply_filters(
'woocommerce_order_item_name', $item->get_name(), $item, false);
echo "<tr class='{$item_class}'>" .
"<td class='qty'>" . esc_html($item->get_quantity()) . "</td>" .
"<td class='name'>";
if ($product) {
$sku = $product->get_sku();
$edit_link = get_edit_post_link($product->get_id());
if (wc_product_sku_enabled() && $sku !== '') { echo "{$sku} - "; }
echo "<a href='{$edit_link}'>{$item_name}</a>";
} else {
echo $item_name;
}
if (!empty($item_meta_html)) {
echo wc_help_tip($item_meta_html);
}
echo "</td>";
}
echo "</table>";
}
}
}
}
add_filter('manage_edit-shop_order_columns', array('ThemeWooCommerce', 'add_purchased_column_header'));
add_action('manage_shop_order_posts_custom_column', array('ThemeWooCommerce', 'render_purchased_column'));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment