在 Woocommerce 3 中访问受订单项保护的数据

Accessing Order Items protected data in Woocommerce 3(在 Woocommerce 3 中访问受订单项保护的数据)
本文介绍了在 Woocommerce 3 中访问受订单项保护的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试获取订单的行项目.

我正在这样做:

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

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

[meta_data:protected] =>大批

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

谢谢.

解决方案

从 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 getter 方法:

//获取 WC_Order 对象的实例$order = wc_get_order(147);//遍历每个订单项foreach ($order->get_items() as $item_id => $item ) {echo $item->get_type().'
';//订单项类型echo $item->get_product_id().'
';//产品 IDecho $item->get_variation_id().'
';//变体IDecho $item->get_quantity().'
';//订单项数量echo $item->get_subtotal().'
';//行项目小计echo $item->get_total().'
';//行项目总计//关联的产品对象(也不能直接访问哪些属性)echo '

';print_r( $item->get_product() );echo '</pre>';//... 等等 ...## 测试原始输出(受保护)//echo '

';print_r($item);echo '</pre>';}

wc_get_order_item_meta() 功能.在这里,您可以进入wp_woocommerce_order_itemmeta 表并使用相应的meta_key(对于line_item 数据类型项 ID):

//获取 WC_Order 对象的实例$order = wc_get_order(147);//遍历每个订单项foreach ($order->get_items() as $item_id => $item ) {echo wc_get_order_item_meta( $item_id, '_product_id', true).'
';//产品编号echo wc_get_order_item_meta( $item_id, '_variation_id', true).'
';//变量 IDecho wc_get_order_item_meta( $item_id, '_qty', true).'
';//数量echo wc_get_order_item_meta( $item_id, '_line_subtotal', true).'
';//行小计//... 等等 ...## 测试原始输出(受保护的数据)//echo '

';print_r($item);echo '</pre>';}

WC_Data 方法 get_data() 方法(取消对数组中数据的保护):

//获取 WC_Order 对象的实例$order = wc_get_order(147);//遍历每个订单项foreach ($order->get_items() as $item_id => $item ) {//在一个可访问的数组中获取最有用的 Item 产品数据$item_data = $item->get_data();echo $item_data['id'].'
';//订单商品IDecho $item_data['order_id'].'
';//订单编号echo $item_data['product_id'].'
';//产品 IDecho $item_data['variation_id'].'
';//变量 IDecho $item_data['name'].'
';//产品标题(名称)echo $item_data['quantity'].'
';//订单项数量echo $item_data['subtotal'].'
';//行项目小计echo $item_data['total'].'
';//行项目总计//... 等等 ...

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

//获取 WC_Order 对象的实例$order = wc_get_order(147);//遍历每个订单项foreach ($order->get_items() as $item_id => $item ) {echo $item->get_meta('_product_id').'<br>';//产品 IDecho $item->get_meta('_variation_id').'
';//变量 IDecho $item->get_meta('_qty').'
';//订单项数量echo $item->get_meta('_line_subtotal').'<br>';//行项目小计echo $item->get_meta('_line_subtotal_tax').'
';//行项目小计税echo $item->get_meta('_line_total').'<br>';//行项目总计echo $item->get_meta('_line_tax').'<br>';//行项目总税额//变体的产品属性echo $item->get_meta('pa_color').'<br>';//颜色echo $item->get_meta('pa_size').'<br>';//颜色//自定义项目元数据echo $item->get_meta('custom_meta_key').'<br>';//自定义元键可见//... 等等 ...

相关:如何获取 WooCommerce 订单详情

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.

解决方案

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

这篇关于在 Woocommerce 3 中访问受订单项保护的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)