Showing SKU column in woocommerce orders page

[ad_1]

I’m trying to show SKU column for each order in WooCommerce > Orders

I did add the following code

// Add the SKU column to the orders page

add_filter( 'manage_edit-shop_order_columns', 'add_sku_column' );

function add_sku_column( $columns ) {

    $columns['sku'] = __( 'SKU', 'custom-order-page' );

    return $columns;

}



add_action( 'manage_shop_order_posts_custom_column' , 'display_sku_column_value', 10, 2 );

function display_sku_column_value( $column, $post_id ) {

    if ( $column === 'sku' ) {

        $order = wc_get_order( $post_id );

        $items = $order->get_items();

        foreach ( $items as $item ) {

            $product = $item->get_product();

            echo $product->get_sku();

        }

    }

}

but it still not showing ?!

On the other hand when I show Orders notes on the same page it did work

add_filter('manage_edit-shop_order_columns', 'custom_order_column_header');
function custom_order_column_header($columns)
{
    $columns['order_notes'] = __('Orderz Notes', 'custom-order-page');
    return $columns;
}

// Populate custom column with order notes
add_action('manage_shop_order_posts_custom_column', 'custom_order_column_content', 10, 2);
function custom_order_column_content($column, $post_id)
{
    if ($column == 'order_notes') {
        $order = wc_get_order($post_id);
        if ($order) {
            echo $order->get_customer_note();
        }
    }
}

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer