I am very new to Wordpress and Woocommerce. Below is some part of my code that I am using to add a custom metabox to order admin single pages:
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' );
}
In this code, if I use the custom function get_the_order()
under get_order_details, it works. My problem, is that I need to access the WC_Order object $order
in various functions throughout the file.
This is all on the admin end with an already existing order so no new order is being created. I need certain details of the order in one function like shipping details and billing details in another function...so on and so forth.
What am I doing wrong? How can I access the order object from an external custom function?