我使用的是Woocommerce最新版本3.4.2。如何获取某些订单商品元数据并为其分配自定义值?我使用公共数组$ item_product_data_array获取元数据。我需要得到……
的 更新: 强> 自Woocommerce第3版以来,您的代码已经过时了。请参阅:
所以你的代码应该是:
$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].""; } }
它应该更好,但是 $product_mod 没有定义和 $item->get_meta_data() 未使用。
$product_mod
$item->get_meta_data()
现在 的 获取一些自定义元数据 强> ,如果你的定制 的 元键 强> 是 Custom thing ,你会用:
Custom thing
$custom_thing = $item->get_meta('Custom thing');
这应该包含在订单项的foreach循环中,经过测试和工作。
其他一些事情:
$item->get_subtotal();
$item->get_total();
$product->get-price();