1

I am using Get the metadata of an order item in woocommerce 3 answer code:

// Get the $order object from an ID (if needed only)
$order = wc_get_order( $order_id);

// Loop through order line items
foreach( $order->get_items() as $item ){
    // get order item data (in an unprotected array)
    $item_data = $item->get_data();

    // get order item meta data (in an unprotected array)
    $item_meta_data = $item->get_meta_data();

    // get only All item meta data even hidden (in an unprotected array)
    $formatted_meta_data = $item->get_formatted_meta_data( '_', true );

    // Display the raw outputs (for testing)
    echo '<pre>'; print_r($item_meta_data); echo '</pre>';
    echo '<pre>'; print_r($formatted_meta_data); echo '</pre>';
}

I need to extract in below array, the follow info separated by [key]:

CARBOIDRATO

  • [key] => Carboidrato
  • [value] => Arroz Branco COM Feijão
  • [key] => Carboidrato
  • [value] => Arroz Integral à Grega COM Feijão
  • [key] => Carboidrato
  • [value] => Macarrão Alho Poró

PROTEINA

  • [key] => Proteína
  • [value] => Parmegiana Bovina
  • [key] => Proteína
  • [value] => Parmegiana Frango

GUARNIÇÃO

  • [key] => Guarnição
  • [value] => Batata Rosthi Recheada com Requeijão
  • [key] => Guarnição
  • [value] => Farofa

Arrays:

Array
(
    [0] => WC_Meta_Data Object
        (
            [current_data:protected] => Array
                (
                    [id] => 1300
                    [key] => Carboidrato
                    [value] => Arroz Branco COM Feijão
                )

            [data:protected] => Array
                (
                    [id] => 1300
                    [key] => Carboidrato
                    [value] => Arroz Branco COM Feijão
                )

        )

    [1] => WC_Meta_Data Object
        (
            [current_data:protected] => Array
                (
                    [id] => 1301
                    [key] => Carboidrato
                    [value] => Arroz Integral à Grega COM Feijão
                )

            [data:protected] => Array
                (
                    [id] => 1301
                    [key] => Carboidrato
                    [value] => Arroz Integral à Grega COM Feijão
                )

        )

    [2] => WC_Meta_Data Object
        (
            [current_data:protected] => Array
                (
                    [id] => 1302
                    [key] => Carboidrato
                    [value] => Macarrão Alho Poró
                )

            [data:protected] => Array
                (
                    [id] => 1302
                    [key] => Carboidrato
                    [value] => Macarrão Alho Poró
                )

        )

    [3] => WC_Meta_Data Object
        (
            [current_data:protected] => Array
                (
                    [id] => 1303
                    [key] => Proteína
                    [value] => Parmegiana Bovina
                )

            [data:protected] => Array
                (
                    [id] => 1303
                    [key] => Proteína
                    [value] => Parmegiana Bovina
                )

        )

    [4] => WC_Meta_Data Object
        (
            [current_data:protected] => Array
                (
                    [id] => 1304
                    [key] => Proteína
                    [value] => Parmegiana Frango
                )

            [data:protected] => Array
                (
                    [id] => 1304
                    [key] => Proteína
                    [value] => Parmegiana Frango
                )

        )

    [5] => WC_Meta_Data Object
        (
            [current_data:protected] => Array
                (
                    [id] => 1305
                    [key] => Guarnição
                    [value] => Batata Rosthi Recheada com Requeijão
                )

            [data:protected] => Array
                (
                    [id] => 1305
                    [key] => Guarnição
                    [value] => Batata Rosthi Recheada com Requeijão
                )

        )

    [6] => WC_Meta_Data Object
        (
            [current_data:protected] => Array
                (
                    [id] => 1306
                    [key] => Guarnição
                    [value] => Farofa
                )

            [data:protected] => Array
                (
                    [id] => 1306
                    [key] => Guarnição
                    [value] => Farofa
                )

        )

)
Array
(
    [1300] => stdClass Object
        (
            [key] => Carboidrato
            [value] => Arroz Branco COM Feijão
            [display_key] => Carboidrato
            [display_value] => 
Arroz Branco COM Feijão



        )

    [1301] => stdClass Object
        (
            [key] => Carboidrato
            [value] => Arroz Integral à Grega COM Feijão
            [display_key] => Carboidrato
            [display_value] => 
Arroz Integral à Grega COM Feijão



        )

    [1302] => stdClass Object
        (
            [key] => Carboidrato
            [value] => Macarrão Alho Poró
            [display_key] => Carboidrato
            [display_value] => 
Macarrão Alho Poró



        )

    [1303] => stdClass Object
        (
            [key] => Proteína
            [value] => Parmegiana Bovina
            [display_key] => Proteína
            [display_value] => 
Parmegiana Bovina



        )

    [1304] => stdClass Object
        (
            [key] => Proteína
            [value] => Parmegiana Frango
            [display_key] => Proteína
            [display_value] => 
Parmegiana Frango



        )

    [1305] => stdClass Object
        (
            [key] => Guarnição
            [value] => Batata Rosthi Recheada com Requeijão
            [display_key] => Guarnição
            [display_value] => 
Batata Rosthi Recheada com Requeijão



        )

    [1306] => stdClass Object
        (
            [key] => Guarnição
            [value] => Farofa
            [display_key] => Guarnição
            [display_value] => 
Farofa



        )

)
Array
(
)
Array
(
)
Array
(
)
Array
(
)
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
vrec
  • 51
  • 7

1 Answers1

1

As the order item meta data that you want has not a unique meta key (used multiple times), you will use WC_Order_Item get_formatted_meta_data() method, to get your custom order item meta data formatted in an array, as follows:

// Get the $order object from an ID (if needed only)
$order = wc_get_order( $order_id);

$data_output = array(); // Initializing

// Loop through order line items
foreach( $order->get_items() as $item ){
    // get only All item meta data even hidden (in an unprotected array)
    $formatted_meta_data = $item->get_formatted_meta_data( '_', true );


    // Loop through order item custom meta data
    foreach( $formatted_meta_data as $meta_data ){
        // Targettting specific meta data (meta keys)
        if( in_array( $meta_data->key, array('Carboidrato', 'Proteína', 'Guarnição') ) ) {
            // Set data in a formated multidimensional array
            $data_output[$meta_data->key][] = $meta_data->value;
        }
    }
}

## 1. Raw output (for testing): values grouped by metakey
echo '<pre>'; print_r($data_output); echo '</pre>';

## 2. Html Output (display): values grouped by metakey

// Loop through metakey / value pairs
foreach( $data_output as $key => $values ){
    echo '<strong>' .strtoupper($key) . '</strong><br>';

    echo '<ul>';

    // Loop through values for the same meta key
    foreach( $values as $value ) {
        echo '<li>' . $value . '</li>';
    }
    echo '</ul>';
}

Tested and works.

Raw data display for your custom formatted array will be like:

Array
(
    [Carboidrato] => Array
        (
            [0] => Arroz Branco COM Feijão
            [1] => Arroz Integral à Grega COM Feijão
            [2] => Macarrão Alho Poró
        )

    [Proteína] => Array
        (
            [0] => Parmegiana Bovina
            [1] => Parmegiana Frango
        )

    [Guarnição] => Array
        (
            [0] => Batata Rosthi Recheada com Requeijão
            [1] => Farofa
        )

)

For info: when the Order item meta data that you want has unique meta keys, you will use instead the WC_Data get_meta() method as follows:

// Get the $order object from an ID (if needed only)
$order = wc_get_order( $order_id);

// Loop through order line items
foreach( $order->get_items() as $item ){
    // get specific order item data value from specific meta key
    $carboidrato = $item->get_meta('Carboidrato');

   // Output value for that metakey
   echo '<p>' . __('Carboidrato') . ': ' . $carboidrato . '</p>';
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399