【发布时间】:2019-03-08 03:55:45
【问题描述】:
我对 Wordpress 和 Woocommerce 非常陌生。下面是我用来添加自定义元框以订购管理单页的代码的一部分:
add_action('add_meta_boxes', 'new_meta_box');
function new_meta_box(){
add_meta_box(
'new_meta_box',
'New Meta Box',
'new_meta_box_button',
'shop_order',
'side',
'high'
);
}
function new_meta_box_button(){
submit_button('New Meta Box Button', 'primary','new_meta_box_button');
global $post;
$order_id = $post->ID;
$order = wc_get_order($order_id);
$order_number = absint($order->get_order_number());
button_action($order);
}
add_action('save_post','button_action');
function button_action($order){
//unbale to access $order here
if(!isset($_POST['new_meta_box_button'])){
return;
}
get_value($order);
}
function get_value($order){
//unable to access $order here
// var_dump($order) shows nothing
$order_id = $order->get_order_number();
$json = get_json($order_id);
$option_value = get_option( 'option_meta_key' );
}
在这段代码中,如果我使用 get_order_details 下的自定义函数get_the_order(),它可以工作。我的问题是,我需要在整个文件的各种函数中访问 WC_Order 对象$order。
这一切都在管理端,已有订单,因此不会创建新订单。我需要在一个功能中提供订单的某些详细信息,例如在另一个功能中提供运输详细信息和账单详细信息……等等。
我做错了什么?如何从外部自定义函数访问订单对象?
【问题讨论】:
-
据我了解 wc_get_order($order_num) 返回 WC_Order 对象。所以你可以尝试像 $order = get_the_order() 然后 $order->get_some_order_detail(); stackoverflow.com/questions/39401393/…
-
@LoicTheAztec - $post->ID 总是给出当前帖子的 ID。使用 $order = new WC_Order ($post->ID) 不起作用,所以我不得不使用 wc_get_order();
-
@LoicTheAztec - 是的,但它只适用于在 add_action() 中调用的函数......是 wc_get_order() 的范围受限还是其他?
-
@LoicTheAztec:嗨,我已经更新了代码。我基本上是在管理端处理订单。单击 submit_button('New Button') 时,我需要将一些订单详细信息发布到数据库中。我不确定有哪些可用的钩子,所以我正在使用 save_post。您对stackoverflow.com/questions/37772912/… 的回答非常有帮助!
标签: php wordpress woocommerce orders meta-boxes