从 Woocommerce 元数据框访问自定义函数中的订单对象

Accessing the Order object in a custom function from a Woocommerce metabox

我对 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_ORDER 包含所有帐单和运输详细信息。

You are still not explaining what you want to do with this metabox in Order admin pages

以下代码基于您的代码,将向您展示方法(具有真正的功能$order):

// Add a new custom meta box to Admin single order pages
add_action('add_meta_boxes', 'new_meta_box');
    function new_meta_box(){
    add_meta_box( 'new_meta_box',
        __('New Meta Box', 'woocommerce'),
        'new_meta_box_content',
        'shop_order', 'side', 'high'
    );
}

// The content of this new metabox
function new_meta_box_content(){
    global $post; // <=== Alway at the beginning

    // Get an instance of the WC_Order Object
    $order = wc_get_order( $post->ID );

    // TESTING an external custom function (with the WC_Order object as argument)
    echo get_formatted_order_key( $order );

    // Example (testing): Displaying a text field
    woocommerce_wp_text_input( array(
        'id'          => '_my_text_field1',
        'type'        => 'text',
        'label'       => __( 'My field 1', 'woocommerce' ),
        'placeholder' => __( 'My placeholder text 1', 'woocommerce' ),
        'description' => __( 'My Custom description 1: your explanations.', 'woocommerce' ),
        'desc_tip'    => true,
    ) );

    // The nonce field (security): Always when you submit data from custom form fields
    echo '<input type="hidden" name="my_fields_nonce" value="' . wp_create_nonce() . '">';

    // Not really needed as the default "Update" button does the same.
    submit_button(__('Submit'), 'primary', 'new_meta_box_button' ); //  <=== To be removed
}

// Saving the text field value only from shop orders post type admin pages
add_action('save_post_shop_order','save_new_meta_box_content');
function save_new_meta_box_content( $post_id ){
    if ( ! isset( $_POST[ 'my_fields_nonce' ] ) ) {
        return $post_id;
    }

    if ( ! wp_verify_nonce( $_REQUEST[ 'my_fields_nonce' ] ) ) {
        return $post_id;
    }

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
        return $post_id;
    }

    // Everything is secured, we can save the value
    if ( isset( $_POST[ '_my_text_field1' ] ) ) {
        update_post_meta( $post_id, '_my_text_field1', sanitize_text_field( $_POST[ '_my_text_field1' ] ) );
    }
}


// Custom external function with the WC_Order object as argument
function get_formatted_order_key( $order ){
    if( is_a($order, 'WC_Order') && $order_key = $order->get_order_key() ){
        $output = '<p><strong>Order key: </strong>'.$order_key.'</p>';
    } else {
        $output = '<p style="font-weight:bold; color:red;">Something is wrong!</p>';
    }
    return $output;
}

代码进入您的活动子主题(活动主题)的 function.php 文件。已测试并有效。