admin管理员组

文章数量:1317909

I'm trying to display every key and value from WooCommerce get_formatted_meta_data order meta details. This is my code to generate the array:

$order = new WC_Order( $order_id );
    foreach ( $order->get_items() as $item_id => $item ) {
        $item_data = $item->get_data();
        $item_meta_data = $item->get_meta_data();
        $formatted_meta_data = $item->get_formatted_meta_data( ' ', true );
        $array = json_decode(json_encode($formatted_meta_data), true);
        $arrayx = array_values($array);
            foreach($arrayx as $value) {
                $result[]=array($value["display_key"], wp_strip_all_tags($value["display_value"]));
                foreach ($result as $key => $value) {
                    $var_label = $key;
                    $var_value = $value;
                    }
                }
    }

And this is the array I managed to print using above function:

Array
(
    [0] => Array
        (
            [0] => Color
            [1] => Black
        )

    [1] => Array
        (
            [0] => Size
            [1] => M
        )

    [2] => Array
        (
            [0] => Gift
            [1] => Tes 2
        )

)

Now I want to get each value in the array and want to display them like this (displayed in new line):

Color: Black
Size: M
Gift: Tes 2
etc
etc
etc (dynamically inserted by WooCommerce if any or added by other plugins)

This is what I have already tried:

$order = new WC_Order( $order_id );
foreach ( $order->get_items() as $item_id => $item ) {
    $product_id   = $item->get_product_id();
    $quantity     = $item->get_quantity();
    $product_name = $item->get_name();
    $item_data = $item->get_data();
    $item_meta_data = $item->get_meta_data();
    $formatted_meta_data = $item->get_formatted_meta_data( ' ', true );
    $array = json_decode(json_encode($formatted_meta_data), true);

    $arrayx = array_values($array);
    $count=0;
    foreach($arrayx as $value) {
        $result[]=array($value["display_key"], wp_strip_all_tags($value["display_value"]));
        foreach ($result as $key => $value) {
            $var_label = $key;
            $var_value = $value;
            $metadata['count'] = "\r\n".$var_label[0].": ".$var_value[0]."";
            $count++;
        }
    }
           
    $formatted_product_name = wp_strip_all_tags(" *".$product_name."*");
    $product_plus_meta = "*".$formatted_product_name."* ".$metadata['count']."";
    $quantity = $item->get_quantity();
    $output_content.= urlencode("".$quantity."x - ".$product_plus_meta."\r\n");
   }
}
$output_content.="".$price."";

Using the above code, I could only fetch one value and key from the array (only the latest in the array) which is Gift => Tes 2. And the output is something like this:

1x - **Sample Product** 
Gift: Tes 2

Price: $12.00

Instead of this:

1x - **Sample Product**
Color: Black
Size: M
Gift: Tes 2

Price: $12.00

How to get the full list of each existed array key and value and display all of them in new line like above sample?

Thank you very much in advance. Any help would be much appreciated.

I'm trying to display every key and value from WooCommerce get_formatted_meta_data order meta details. This is my code to generate the array:

$order = new WC_Order( $order_id );
    foreach ( $order->get_items() as $item_id => $item ) {
        $item_data = $item->get_data();
        $item_meta_data = $item->get_meta_data();
        $formatted_meta_data = $item->get_formatted_meta_data( ' ', true );
        $array = json_decode(json_encode($formatted_meta_data), true);
        $arrayx = array_values($array);
            foreach($arrayx as $value) {
                $result[]=array($value["display_key"], wp_strip_all_tags($value["display_value"]));
                foreach ($result as $key => $value) {
                    $var_label = $key;
                    $var_value = $value;
                    }
                }
    }

And this is the array I managed to print using above function:

Array
(
    [0] => Array
        (
            [0] => Color
            [1] => Black
        )

    [1] => Array
        (
            [0] => Size
            [1] => M
        )

    [2] => Array
        (
            [0] => Gift
            [1] => Tes 2
        )

)

Now I want to get each value in the array and want to display them like this (displayed in new line):

Color: Black
Size: M
Gift: Tes 2
etc
etc
etc (dynamically inserted by WooCommerce if any or added by other plugins)

This is what I have already tried:

$order = new WC_Order( $order_id );
foreach ( $order->get_items() as $item_id => $item ) {
    $product_id   = $item->get_product_id();
    $quantity     = $item->get_quantity();
    $product_name = $item->get_name();
    $item_data = $item->get_data();
    $item_meta_data = $item->get_meta_data();
    $formatted_meta_data = $item->get_formatted_meta_data( ' ', true );
    $array = json_decode(json_encode($formatted_meta_data), true);

    $arrayx = array_values($array);
    $count=0;
    foreach($arrayx as $value) {
        $result[]=array($value["display_key"], wp_strip_all_tags($value["display_value"]));
        foreach ($result as $key => $value) {
            $var_label = $key;
            $var_value = $value;
            $metadata['count'] = "\r\n".$var_label[0].": ".$var_value[0]."";
            $count++;
        }
    }
           
    $formatted_product_name = wp_strip_all_tags(" *".$product_name."*");
    $product_plus_meta = "*".$formatted_product_name."* ".$metadata['count']."";
    $quantity = $item->get_quantity();
    $output_content.= urlencode("".$quantity."x - ".$product_plus_meta."\r\n");
   }
}
$output_content.="".$price."";

Using the above code, I could only fetch one value and key from the array (only the latest in the array) which is Gift => Tes 2. And the output is something like this:

1x - **Sample Product** 
Gift: Tes 2

Price: $12.00

Instead of this:

1x - **Sample Product**
Color: Black
Size: M
Gift: Tes 2

Price: $12.00

How to get the full list of each existed array key and value and display all of them in new line like above sample?

Thank you very much in advance. Any help would be much appreciated.

Share Improve this question edited Oct 28, 2020 at 15:13 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Oct 28, 2020 at 12:19 Walter P.Walter P. 787 bronze badges 1
  • I think the problem is $metadata['count'], which is where you're saving one value only and then overwriting it with the next one. Did you mean $count? But then I'm not sure what the $metadata array is, and you'll need to change the way you display that later. Or you could just append to any existing $metadata['count'] value rather than overwriting. – Rup Commented Oct 28, 2020 at 23:20
Add a comment  | 

2 Answers 2

Reset to default 1

if you can print your array why cant' you just do a loop on this array and adding each line into your output_content. It will look like this

<?php
$myArray = Array
(
    0 => Array
        (
            0 => "Color",
            1 => "Black",
        ),

    1 => Array
        (
            0 => "Size",
            1 => "M",
        ),

    2 => Array
        (
            0 => "Gift",
            1 => "Tes 2"
        ),

);
$output_content = "";
foreach($myArray as $key){
    $output_content .= $key[0]." : ".$key[1]."\r";
}
echo $output_content;

?>

And this code prints :

Color : Black
Size : M
Gift : Tes 2

By using @Djilan's answer, I managed to fetch the data accordingly and tweak how it will be displayed, preventing compounded array in the next item.

I missed this part (answered by @Djilan), and this is indeed the correct way:

$product_meta = "";
foreach($myArray as $key){
    $product_meta .= $key[0]." : ".$key[1]."\r";
}
echo $product_meta ;

Just need to adjust the code, and this is the final code:

...
...
$order = new WC_Order( $order_id );
foreach ( $order->get_items() as $item_id => $item ) {
    $product_id   = $item->get_product_id(); //Get the product ID
    $quantity     = $item->get_quantity(); //Get the product QTY
    $product_name = $item->get_name(); //Get the product NAME
    $quantity = $item->get_quantity();
    $output_content .= urlencode("".$quantity."x - *".$product_name."*\r\n");
        // get order item data
        $item_data = $item->get_data();

        // get order item meta data
        $item_meta_data = $item->get_meta_data();

        // get only All item meta data
        $formatted_meta_data = $item->get_formatted_meta_data( '_', true );
        $array = json_decode(json_encode($formatted_meta_data), true);
        $arrayx = array_values($array);
        $arrayxxx = array_merge($array);
        $result = array();
    foreach( (array) $arrayxxx as $value) {
        $product_meta = "";
        $result[]=array($value["display_key"], wp_strip_all_tags($value["display_value"]));
        foreach ($result as $key) {
            $result = array();
            $product_meta .= "     - ".$key[0].": ".$key[1]."\r\n";
            $output_content.= urlencode("".$product_meta."");
       }
    }
}
$output_content.="".$format_subtotal_price."";
echo $output_content;

And the final output would be something like this:

1x - Sample Product #1 - Black, M
     - Color: Black
     - Size: M
1x - Sample Product #2
     - Color: Blue
     - Size: XL
     - Gift: Tes 3
$1,173.00

本文标签: pluginsGet Every Key amp Value from Array then Display All in New Line