我已经使用wc_get_orders($ order_id)获得了订单并得到了对象。看起来像这样:
$customer_orders = wc_get_orders( $order_id);
数据:
{
"id":1669,
"parent_id":0,
"status":"completed",
"cart_hash":"8daaeb335972b31eb65f46bafb73bf39",
"number":"1669",
"meta_data":[
{
"id":4935,
"key":"is_vat_exempt",
"value":"no"
},
{
"id":4936,
"key":"domain_name",
"value":"mywebsite.com"
},
{
"id":4948,
"key":"slide_template",
"value":"default"
}
]
}
我删除了不必要的细节。我想在meta_data数组下获取“ key:domain_name”的“值”。
如何访问?
答案 0 :(得分:2)
meta_data
没有密钥,因此您需要循环槽。
$customer_orders = wc_get_orders( $order_id);
$domain = get_meta_item_value($customer_orders, 'domain_name');
function get_meta_item_value($ob, $key, $result = '') {
foreach ($ob->meta_data as $meta_item) {
if ($meta_item->key == $key) {
$result = $meta_item->value;
}
}
return $result;
}