Access secure order data in Woocommerce 3

I am trying to get order items.

I'm doing it:

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

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

[meta_data:protected] => Array

How to access this array to get values?

Thank.

+3
source share
2 answers

Starting with WooCommerce 3.0+, a new Object class appears for Order elements . Now Order Order properties cannot be accessed directly, as before WC_Order_Item_Product

So, if you look at your source data, you will see that each position is now an object, and you can access this protected data using exclusively:

WC_Order_Item_Product getters:

// 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_obj) {
    echo $item_obj->get_type().'<br>'; // The order item type
    echo $item_obj->get_product_id().'<br>'; // The Product ID
    echo $item_obj->get_variation_id().'<br>'; // The variation ID
    echo $item_obj->get_quantity().'<br>'; // Line item quantity
    echo $item_obj->get_subtotal().'<br>'; // Line item subtotal
    echo $item_obj->get_total().'<br>'; // Line item total

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

    // ... and so on ...

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

wc_get_order_item_meta(). wp_woocommerce_order_itemmeta meta_key ( line_item ):

// 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_obj) {

    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 
    // echo '<pre>'; print_r($item_obj); echo '</pre>';
}

WC_Data ( get_data()):

// 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_obj) {

    // Get the most useful Item product data in an accessible array
    $item_data = $item_obj->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 ...

: WooCommerce

+12

[meta_data:protected] => Array, .

$item_obj->get_meta_data();

, :

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

+1

Source: https://habr.com/ru/post/1669685/


All Articles