Accessing Order Items protected data in Woocommerce 3

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


    // ... and so on ...

Related: How to get WooCommerce order details

Leave a Comment