【问题标题】:Accessing the Order object in a custom function from a Woocommerce metabox从 Woocommerce 元框访问自定义函数中的 Order 对象
【发布时间】: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


【解决方案1】:

你仍然没有在订单管理页面中解释你想用这个元框做什么

以下代码基于您的代码,将为您指明方向(带有真正功能的$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 文件中。经过测试并且可以工作。

【讨论】:

  • 感谢您的回答。这确实有很大帮助。唯一的事情是我需要按钮的行为类似于订单注释元框中的“添加”按钮。所以,我不希望在单击按钮时重新加载整个页面。相反,它可以只显示处理微调器,然后使用消息更新页面。我用什么钩子呢?我会为此使用 add_action 或 add_filter 吗?谢谢!
  • 我有同样的q。
【解决方案2】:

WC_ORDER 包含所有帐单和运输详细信息。

【讨论】:

  • 我正在处理已有订单的管理员端。我需要在不同的功能中使用不同的订单详细信息....说一个功能中的所有运输详细信息和另一个功能中的所有账单详细信息。
猜你喜欢
  • 2016-12-22
  • 1970-01-01
  • 1970-01-01
  • 2011-11-09
  • 2019-03-02
  • 1970-01-01
  • 2016-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多