• Resolved bahnsawy

    (@bahnsawy)


    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();
            }
        }
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter bahnsawy

    (@bahnsawy)

    Here’s how I did fix this issue

    I did combine both of them

    add_filter('manage_edit-shop_order_columns', 'custom_order_column_header');
    function custom_order_column_header($columns)
    {
        $columns['order_notes'] = __('Order Notes', 'custom-order-notes');
    	$columns['order_sku'] = __('SKU', 'custom-order-notes');
        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();
            }
        }
    	
    	if ($column == 'order_sku') {
            $order = wc_get_order( $post_id );
            $items = $order->get_items();
            foreach ( $items as $item ) {
                $product = $item->get_product();
                echo $product->get_sku() . '<br/>';
            }
    
            
        }
    }

    you can add your own validation to check if the $product->get_sku() isset

    Hey there, @bahnsawy! Thanks for contacting us.

    Thank you for sharing the solution here and contributing to the WooCommerce Community. This might help other users in the future.

    If you need further help with custom code, you can visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there too.

    Please let us know if there’s anything else we can do to help or if you have any questions.

    Have a wonderful day!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Showing SKU column in woocommerce orders page’ is closed to new replies.