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

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能