WooCommerce Order overview add column with ordered products

I’m trying to improve WooCommerce’s Order overview screen, I would like to add an column with ordered products.

So I can see for example this columns:
Order #, Order Total, Ordered products, Address, Notes, Action

Read More

We have already found some code on the internet to add an column with products, but it is missing products SKU. What I can see currently:

  • 1x ProductnameA
  • 3x ProductnameC

What I would like to see:

  • 1x ARM-002 (ProductnameA)
  • 3x ARM-008 (ProductnameC)

I have used this code for adding the column:

add_filter('manage_edit-shop_order_columns', 'add_ordered_products_column', 11);    
function add_ordered_products_column($columns) {
    $columns['order_products'] = "Ordered products";
    return $columns;
}

And this one for adding the content of the column:

add_action( 'manage_shop_order_posts_custom_column' , 'add_ordered_products_column_content', 11, 2 );
function add_ordered_products_column_content( $column ) {
 global $post, $woocommerce, $the_order;

    switch ( $column ) {

        case 'order_products' :

        $terms = $the_order->get_items();

        if ( is_array( $terms ) ) {
             foreach($terms as $term) {
                echo $term['item_meta']['_qty'][0] .' x '. $term['name'] .'<br />';
            }
        }

        else {
                _e( 'Unable to get products', 'woocommerce' );
        }

        break;
    }
}

I would like to use something like $term[‘sku’], but that doesn’t work, neither get_sku();.
Anyone who knows a solution for this problem?

Related posts

Leave a Reply

3 comments

  1. First of all if you’re looking for products SKU, replace this line of code:

    echo $term['item_meta']['_qty'][0] .' x '. $term['name'] .'<br />';
    

    with this:

    $sku = ( $sku = get_post_meta( $term['product_id'], '_sku', true ) ) ? $sku : '';
    echo $term['quantity'] . ' ' . $sku . ' x ' . $term['name'] .'<br />';
    

    If after that something is still wrong, I recommend you to check this code which works for me.

  2. Have you tried with something like

    <?php echo ( $sku = $_product->get_sku() ) ? $sku : __( 'n/a', 'woocommerce' ); ?>
    

    Where

    <?php 
         foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
             $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
             if( necessary conditionals goes here){ ... }        
         }
    ?>
    
  3. Please note as of Woocommerce 3.0 that this line of code:

    echo $term['item_meta']['_qty'][0] .' x '. $term['name'] .'<br />';
    

    no longer displays the qty of the item ordered.

    You have to exchange that for this line of code if you’re on Woocommerce 3.0 and higher:

    echo $term['quantity'] .' x '. $term['name'] .'<br />';