WooCommerce 3+ WC_Order in backend

Viewed 910

I'm having an issue with a function in Woocommerce 3.X. I (think I) understand it is because WC_Order can no longer be accessed directly but I am unsure how to fix it in the function (which I didn't write).

//Admin JS
    add_action('admin_enqueue_scripts', 'admin_hooks');
    function admin_hooks( $hook ) {

        global $woocommerce, $post;
        $order = new WC_Order($post->ID);
        //to escape # from order id
        $order_id = trim(str_replace('#', '', $order->get_order_number()));
        $user_id = $order->user_id;
        $user_info = get_userdata($user_id);


        wp_enqueue_script( 'admin-hooks', get_template_directory_uri(). '/js/admin.hook.js' );
        wp_localize_script( 'admin-hooks', 'myTest', $user_info->roles );
    }

I tried changing $order = new WC_Order($post->ID); to $order = new wc_get_order( $order_id ); with no luck, which kind of makes sense. I can see I am trying to get the post id not the order id, just not sure how to. As you can see I'm just getting my head around code, so go easy. I did see this post but couldn't work out how to implement with my code, any input appreciated.

Just to give a quick bit of feedback about what the function does, it shows the logged in users role on the admin order page.

1 Answers
Related