yan-k
4/19/2017 - 8:15 PM

Add WooCommerce Purchased Column for WooCommerce 3.0+

Add WooCommerce Purchased Column for WooCommerce 3.0+

<?php

/**
 * Add WooCommerce Purchased Column for WooCommerce 3.0+
 */
add_filter('manage_edit-shop_order_columns', 'yanco_wc_custom_purchased_column');
function yanco_wc_custom_purchased_column( $columns ) {
    $new_array = array();
    foreach ($columns as $key => $title) {
        if ($key == 'billing_address') {

            $new_array['order_items'] = __('Purchased', 'woocommerce');
        }

        $new_array[$key] = $title;
    }
    return $new_array;
}

add_action('manage_shop_order_posts_custom_column', 'yanco_wc_shop_custom_column', 10, 2);
function yanco_wc_shop_custom_column( $column ) {
    global $post, $woocommerce, $the_order;
    switch ($column) {

        case 'order_items':
            $terms = $the_order->get_items();

			echo '<a href="#" class="show_order_items">' . apply_filters( 'woocommerce_admin_order_item_count', sprintf( _n( '%d item', '%d items', $the_order->get_item_count(), 'woocommerce' ), $the_order->get_item_count() ), $the_order ) . '</a>';

				if ( sizeof( $the_order->get_items() ) > 0 ) {

					echo '<table class="order_items" cellspacing="0">';

					foreach ( $the_order->get_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 );
						?>
						<tr class="<?php echo apply_filters( 'woocommerce_admin_order_item_class', '', $item, $the_order ); ?>">
							<td class="qty"><?php echo esc_html( $item->get_quantity() ); ?></td>
							<td class="name">
								<?php  if ( $product ) : ?>
									<?php echo ( wc_product_sku_enabled() && $product->get_sku() ) ? $product->get_sku() . ' - ' : ''; ?><a href="<?php echo get_edit_post_link( $product->get_id() ); ?>"><?php echo apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ); ?></a>
								<?php else : ?>
									<?php echo apply_filters( 'woocommerce_order_item_name', $item->get_name(), $item, false ); ?>
								<?php endif; ?>
								<?php if ( ! empty( $item_meta_html ) ) : ?>
									<?php echo wc_help_tip( $item_meta_html ); ?>
								<?php endif; ?>
							</td>
						</tr>
						<?php
					}
					echo '</table>';
				} else echo '&ndash;';
			break;
		}
}