Question

I need to display product sales quantity on my orders backoffice. Someone can give some help with this?

I can´t find any solution for this..

I already have this:

add_filter( 'manage_edit-shop_order_columns', 'admin_orders_list_add_column', 10, 1 );
function admin_orders_list_add_column( $columns ){
    $columns['custom_column2'] = __( 'Quantidade', 'woocommerce' );

    return $columns;
}


add_action( 'manage_shop_order_posts_custom_column' , 'admin_orders_list_column_content', 10, 2 );
function admin_orders_list_column_content( $column, $post_id ){

    global $post;

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

    $order = wc_get_order( $post->ID );
    echo '<p>Qty: ' . $order->get_items() . '</p>';
}
}

But is not working..

image

Was it helpful?

Solution

Please try with this, I hope it will be help, I did try with this code. It's work for me.

add_filter( 'manage_edit-shop_order_columns', 'admin_orders_list_add_column', 10, 1 );
function admin_orders_list_add_column( $columns ){
    $columns['custom_column2'] = __( 'Quantidade', 'woocommerce' );

    return $columns;
}

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

function admin_orders_list_column_content( $column){

  global $the_order; // the global order object

    if ( 'custom_column2' === $column ) {
        // get items from the order global object
        $order_items = $the_order->get_items();

        if ( !is_wp_error( $order_items ) ) {

            foreach( $order_items as $order_item) {
                $order_count[$order_item["product_id"]] = $order_item["quantity"];
                $total_order = array_sum($order_count);
            }

            echo '<p>Qty: ' . $total_order . '</p>';
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top