在 Woocommerce 中访问和显示订单项元数据 [英] Access and display order item meta data in Woocommerce

查看:53
本文介绍了在 Woocommerce 中访问和显示订单项元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Woocommerce 中,我试图显示订单项对象的结果并访问它:

$product_meta = $item->get_meta_data();print_r ($product_meta);

这就是我想要拉的:

这是我使用 $item->get_formatted_meta_data( '', true ) 得到的输出:

解决方案

要获取所有订单项元数据,您将使用 WC_Order_Item get_formatted_meta_data() 方法具体参数,这样:

//可访问的非保护订单项元数据$item_meta_data = $item->get_formatted_meta_data( '', true );//格式化的原始输出echo '

';打印_r($item_meta_data);echo '</pre>';

要访问某些订单项属性,您可以使用任何WC_Order_Item_Product 方法 如:

$item->get_product();//获取 WC_Product 对象$item->get_product_id();//获取产品ID$item->get_variation_id();//获取变量ID$item->get_name();//获取产品名称$item->get_quantity();//获取商品数量//等等 …

然后,如果您需要访问特定的自定义"订单商品数据值,您将使用 WC_Data get_meta() 方法:

$custom_value = $item->get_meta("_custom_key");

参见:获取在 Woocommerce 3 中订购商品和 WC_Order_Item_Product

<小时>

更新 (显示您所需的自定义订单商品元数据)

您需要的数据可以通过这种方式访问​​和显示:

if( $lessons = $item->get_meta('lessons') ) {echo '<p>课程:'.$lessons.'</p>';}if( $tour_guide = $item->get_meta('tour guide') ) {echo '<p>导游:'.$tour_guide.'</p>';}

我希望这现在有效.

In Woocommerce I am trying to display the results of order item object and to access it:

$product_meta = $item->get_meta_data();
print_r ($product_meta);

This is what I am trying to pull:

EDIT: This is the output that I get using $item->get_formatted_meta_data( '', true ):

解决方案

To get all order item meta data, you will use WC_Order_Item get_formatted_meta_data() method with specific arguments, this way:

// Accessible non protected Order item meta data
$item_meta_data = $item->get_formatted_meta_data( '', true );

// Formatted raw Output
echo '<pre>'; print_r($item_meta_data); echo '</pre>';

To access some order item properties, you can use any WC_Order_Item_Product method like:

$item->get_product(); // Get the WC_Product object

$item->get_product_id(); // Get the Product ID

$item->get_variation_id(); // Get the Variation ID

$item->get_name(); // Get the Product name

$item->get_quantity(); // Get the item quantity 

// and so on …

Then if you need to access a specific "custom" order item data value, you will use WC_Data get_meta() method:

$custom_value = $item->get_meta("_custom_key");

See: Get Order items and WC_Order_Item_Product in Woocommerce 3


Update (displaying your required custom order item meta data)

The data you need can be accessed and displayed this way:

if( $lessons = $item->get_meta('lessons') ) {
    echo '<p>Lessons: '.$lessons.'</p>';
}

if( $tour_guide = $item->get_meta('tour guide') ) {
    echo '<p>Tour Guide: '.$tour_guide.'</p>';
}

I hope that this works now.

这篇关于在 Woocommerce 中访问和显示订单项元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆