0

I'm trying to loop through all completed orders and print out their meta key and value, I can get the completed orders ID and bring back all the meta keys but not the values.

I'm new to PHP and woocommerce, so guess I'm missing something stupid!?

<?php
/**
* Template Name:test List
*/
wp_head();
 
 Echo "testing";
 
$args = array(
    'status'       => 'completed', 
    //'meta_key'     => 'arrival_date', 
    //'meta_value'   => $today, 
    //'meta_compare' => '<=', 
);

$orders = wc_get_orders( $args );

foreach ( $orders as $order ) {
    $order_id = $order->get_id();
    $order_meta = get_post_meta($order_id);
        echo '<p>' . $order_id . '</p>';
        foreach($order_meta as $key=>$val){
            echo $key;
            echo $val;
            //echo is_array($val)?current($val):$val;
            echo "<br>";
        }
    echo "<br>";
    
    
}

//echo '<pre>'; print_r($array); echo '</pre>';

wp_footer();
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Adrian
  • 1,089
  • 24
  • 51
  • Instead of `get_post_meta($order_id)` use `$data = $order->get_data()` and `$meta_data = $order->get_meta_data()`… Also to get a specific meta data value from a meta key, use `$value = $order->get_meta('_my_meta_key')`; – LoicTheAztec Oct 04 '20 at 20:42
  • 1
    thank you, $value = $order->get_meta('_my_meta_key'); is perfect for what I wanted to do. – Adrian Oct 04 '20 at 21:01
  • Happy that it helped you… But don't forget that you should use [`WC_Order` methods](https://github.com/woocommerce/woocommerce/blob/4.5.2/includes/class-wc-order.php) or [`WC_Abstract_Order` methods](https://github.com/woocommerce/woocommerce/blob/4.5.2/includes/abstracts/abstract-wc-order.php) for default WooCommerce meta data, instead of `WC_Data` `get_meta()` method. – LoicTheAztec Oct 04 '20 at 21:10

0 Answers0