Get custom order item metadata in Woocommerce 3

本秂侑毒 提交于 2019-12-04 18:30:32

Updated: Your code is really outdated since Woocommerce version 3… See:

So your code should be:

$skus = $item_quantities = $line_item_totals = $items_meta_data = array();

// Loop though order items
foreach( $order->get_items() as $item_id => $item){
    $product_id = $item->get_product_id();
    $product = $item->get_product();

    $item_quantities[] = $item->get_quantity();
    $line_item_totals[] = $item->get_total();
    $skus[]            = $product->get_sku();
    $items_meta_data[]  = $item->get_meta_data();
}

// Product details for sending as one line to an external service
foreach ($skus as $key => $value){
    $data .= "&product[".$key."]=".$value."";
    $data .= "& product_kol[".$key."]=".$item_quantities[$key]."";
    $data .= "& product_price[".$key."]=".$line_item_totals[$key]."";
    if( isset($product_mod[$key]) ) {
        $data .= "&product_mod[".$key."]=".$product_mod[$key]."";
    }
}

It should work better… but $product_mod is not defined and $item->get_meta_data() is not used.


Now To get some custom meta data, if your custom meta key is Custom thing, you will use:

 $custom_thing = $item->get_meta('Custom thing');

This should be included in the foreach loop of order items… Tested and works.


Some other things:

  • To get the NON discounted order line item total: $item->get_subtotal();
  • To get the discounted order line item total: $item->get_total();
  • To get the product price (unit): $product->get-price();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!