doushouxie7064 2019-03-15 12:27
浏览 112
已采纳

在Woocommerce中访问和显示订单商品元数据

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:

enter image description here

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

enter image description here

  • 写回答

2条回答 默认 最新

  • dtqpw68806 2019-03-15 12:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改