使用非唯一元键获取特定的 WooCommerce 订单项元数据

Get specific WooCommerce order item metadata with non unique meta keys

我正在使用 答案代码:

// 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>';
}

我需要在下面的数组中提取,以下信息由 [key] 分隔:

碳水化合物

蛋白质

服装

数组:

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
(
)

由于您想要的订单项元数据没有唯一的元键(多次使用),您将使用 WC_Order_Item get_formatted_meta_data() 方法来格式化您的自定义订单项元数据在数组中,如下:

// 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>';
}

已测试并有效。

自定义格式数组的原始数据显示如下:

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
        )

)

参考信息: 当您想要的订单项元数据 具有唯一的元键 时,您将使用 WC_Dataget_meta()方法如下:

// 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>';
}