2

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Nik
  • 671
  • 1
  • 8
  • 27
  • `$order->user_id` needs to be replaced by `$order->get_user_id()` for WooCommerce 3.0.x. Object properties can no longer be directly accessed, so you aren't getting any user info. If you turn on `DEBUG_LOG` you would probably see a bunch of notifications about this. – helgatheviking Jul 21 '17 at 18:02

1 Answers1

1

You can only get the post ID, in backend, in "post edit pages", so for Orders it will be the "order edit pages" (but not the "Orders list pages").

In your function hooked in admin_enqueue_scripts you need to target only Order edit pages.

You don't need to get the WC_Order object and for order pages the Order ID is the Post ID.

The User ID in Orders is the Customer ID ('customer' user role in general).
Also, for info, $user_info->roles; is an array!

So The correct code will be:

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

    // Targeting only post edit pages
    if ( 'post.php' != $hook && ! isset($_GET['post']) && ! $_GET['action'] != 'edit' )
        return;

    // Get the post ID
    $post_id = $_GET['post']; // The post_id

    // Get the WP_Post object
    $post = get_post($post_id);

    // Targeting only Orders
    if( $post->post_type != 'shop_order' )
        return;

    // Get the customer ID (or user ID for customer user role)
    $customer_id      = get_post_meta( $post_id, '_customer_user', true ); 
    $user_info        = get_userdata($customer_id);

    $user_roles_array = $user_info->roles; // ==> This is an array !!!

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

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

I can't test this code as it involve other external files. But it should work.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • That's awesome, thanks Loic! That makes a lot more sense, I didn't think it needed to run on all admin pages, the comments and explanation are really helpful! Do you freelance? – Nik Jul 22 '17 at 01:53