wordpress/woocommerce - sum of custom fields

Paweł Skaba

I add an input field to my woocommerce (which is linked with each order). I can perfectly display each value of that field using below code:

function MY_COLUMNS_VALUES_FUNCTION( $column ) {
    global $post;
    $order = new WC_Order( $post->ID );
    $items = $order->get_items();

    //start editing, I was saving my fields for the orders as custom post meta
    //if you did the same, follow this code
    if ( $column == 'authors_income' ) {
        foreach ( $items as $item ) {

            echo $item['MY-FIELD-NAME'];

        }
    }

But how can i sum all the values of that field and echo/print it?

AgeDeO

Just add the current value to a variable and echo the result after the loop

if ( $column == 'authors_income' ) {
   $totalIncome = 0;
    foreach ( $items as $item ) {

        echo $item['MY-FIELD-NAME'];
        $totalIncome = $totalIncome + $item['MY-FIELD-NAME'];

    }
    echo $totalIncome;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related