【问题标题】:Accessing Order Items protected data in Woocommerce 3在 Woocommerce 3 中访问受订单项保护的数据
【发布时间】:2017-09-21 16:33:32
【问题描述】:

我正在尝试获取订单的订单项。

我正在这样做:

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

我可以看到我需要的所有数据,但数组显示如下:

[meta_data:protected] => Array

如何访问该数组以获取值?

谢谢。

【问题讨论】:

  • 我可以通过以下方式访问它:$lineItem->get_meta_data()

标签: php wordpress class woocommerce orders


【解决方案1】:

由于订单商品的 WooCommerce 3.0+ 有新的对象类 WC_Order_Item_Product
现在无法像以前一样直接访问订单商品属性

因此,如果您查看输出的原始数据,您会发现每个订单项现在都是一个对象,并且您将能够以独占方式访问受保护的数据:

  1. WC_Order_Item_Product getters 方法(或用 setters 方法改变它)...
  2. WC_Order_Item get_formatted_meta_data( '', true ) 方法来访问所有元数据。它提供了一组可访问对象。请参阅WC_Data 方法get_meta() 以访问每个元数据。
  3. WC_Data getters 方法取消保护此数据并使用以下方法通过数组访问它:
    • get_data() (这个方法很有用)
    • get_meta() (这个方法最有用)
    • get_data_keys()
    • get_meta_data() 不取消保护数据,使用get_formatted_meta_data()
  4. wc_get_order_item_meta() 专用功能。

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 ) {
    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>';
}

wc_get_order_item_meta() 函数。在这里,您可以进入 wp_woocommerce_order_itemmeta 表并使用相应的 meta_key 输出项目 ID 的任何数据(对于 line_item 数据类型项目 ID): p>

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

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

    // 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 ...

WC_Data 方法 get_meta() 方法(通过元键访问每个属性):

// 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 ...

相关:How to get WooCommerce order details

【讨论】:

  • 谢谢。我希望这在文档中更清楚。
【解决方案2】:

要获取[meta_data:protected] =&gt; Array的数据,需要使用其他方法。

就用这个$item_obj-&gt;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>';
            }
        }
  }

方法集合位于here

【讨论】:

  • 在 wocommerce 3+ 中没有打印
猜你喜欢
  • 2019-03-25
  • 2019-03-31
  • 2019-06-14
  • 2018-07-11
  • 2019-01-28
  • 1970-01-01
  • 2010-10-04
  • 1970-01-01
相关资源
最近更新 更多