Accessing Order Items protected data in Woocommerc

2019-01-23 21:18发布

问题:

I am trying to get the line items of an order.

I'm doing this:

$order = new WC_Order(147);
foreach ($order->get_items() as $key => $lineItem) {
    print_r('<pre>----');
    print_r($lineItem);
    print_r('----</pre>');
}

I can see all the data I need but the array shows this:

[meta_data:protected] => Array

How can I access this array to get the values?

Thanks.

回答1:

Since WooCommerce 3.0+ for Order items there is new Object class WC_Order_Item_Product.
Now Order items properties can't be accessed directly as before

So if you look at your output raw data you will see that each line item is now an object, and you will be able to access that protected data using exclusively:

  1. WC_Order_Item_Product getters methods (or to change it with the setters methods)…
  2. WC_Order_Item get_formatted_meta_data( '', true ) method to access all meta data. It gives an array of accessible objects. See WC_Data method get_meta() to access each meta data.
  3. WC_Data getters methods to unprotect this data and access it through arrays using methods:
    • get_data() (this method is the very useful)
    • get_meta() (this method is the most useful)
    • get_data_keys()
    • get_meta_data() (does not unprotect the data, use get_formatted_meta_data())
  4. wc_get_order_item_meta() dedicated function.

The WC_Order_Item_Product getters methods:

// Get an instance of the WC_Order object
$order = wc_get_order(147);

// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {
    echo $item->get_type().'<br>'; // The order item type
    echo $item->get_product_id().'<br>'; // The Product ID
    echo $item->get_variation_id().'<br>'; // The variation ID
    echo $item->get_quantity().'<br>'; // Line item quantity
    echo $item->get_subtotal().'<br>'; // Line item subtotal
    echo $item->get_total().'<br>'; // Line item total

    // The associated product object (which properties can't be accessed directly too)
    echo '<pre>'; print_r( $item->get_product() ); echo '</pre>'; 

    // ... and so on ...

    ## Testing raw output (protected)
    // echo '<pre>'; print_r($item); echo '</pre>';
}

The wc_get_order_item_meta() function. Here you can go in wp_woocommerce_order_itemmeta table and output any data for an item ID using the corresponding meta_key (for line_item data type item ID):

// Get an instance of the WC_Order object
$order = wc_get_order(147);

// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {

    echo wc_get_order_item_meta( $item_id, '_product_id', true). '<br>'; // Product ID
    echo wc_get_order_item_meta( $item_id, '_variation_id', true). '<br>'; // Variation ID
    echo wc_get_order_item_meta( $item_id, '_qty', true). '<br>'; // quantity
    echo wc_get_order_item_meta( $item_id, '_line_subtotal', true). '<br>'; // Line subtotal

    // ... and so on ...

    ## Testing raw output (protected data)
    // echo '<pre>'; print_r($item); echo '</pre>';
}

The WC_Data method get_data() method (to unprotect the data in an array):

// Get an instance of the WC_Order object
$order = wc_get_order(147);

// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {

    // Get the most useful Item product data in an accessible array
    $item_data = $item->get_data();

    echo $item_data['id'].'<br>'; // The order item ID
    echo $item_data['order_id'].'<br>'; // The order ID
    echo $item_data['product_id'].'<br>'; // The Product ID
    echo $item_data['variation_id'].'<br>'; // The Variation ID
    echo $item_data['name'].'<br>'; // The Product title (name)
    echo $item_data['quantity'].'<br>'; // Line item quantity
    echo $item_data['subtotal'].'<br>'; // Line item subtotal
    echo $item_data['total'].'<br>'; // Line item total

    // ... and so on ...

The WC_Data method get_meta() method (to access each property by its meta key):

// Get an instance of the WC_Order object
$order = wc_get_order(147);

// Iterating through each order item
foreach ($order->get_items() as $item_id => $item ) {

    echo $item->get_meta('_product_id').'<br>'; // The Product ID
    echo $item->get_meta('_variation_id').'<br>'; // The Variation ID
    echo $item->get_meta('_qty').'<br>'; // Line item quantity
    echo $item->get_meta('_line_subtotal').'<br>'; // Line item subtotal
    echo $item->get_meta('_line_subtotal_tax').'<br>'; // Line item subtotal tax
    echo $item->get_meta('_line_total').'<br>'; // Line item total
    echo $item->get_meta('_line_tax').'<br>'; // Line item total tax

    // Product attributes for variation
    echo $item->get_meta('pa_color').'<br>'; // Color
    echo $item->get_meta('pa_size').'<br>'; // Color

    // Custom item meta gata
    echo $item->get_meta('custom_meta_key').'<br>'; // custom meta key visible
    echo $item->get_meta('_custom_meta_key').'<br>'; // custom meta key not visible


    // ... and so on ...

Related: How to get WooCommerce order details



回答2:

To get the data of [meta_data:protected] => Array You need to use another method.

Just use this $item_obj->get_meta_data();

A more detailed way to get them, iterate them twice as follows:

  $order = wc_get_order( $order_id );
  foreach ($order->get_items() as $item_id => $item_obj) {

         $kua = $item_obj->get_meta_data();

         foreach ($kua as $key => $value) {
            foreach ($value as $key2 => $value2) {
               echo $key2.'->'.$value2.'<br>';
            }
        }
  }

Collection of the methods is located here