2017-07-21 7 views
2

私はWoocommerce 3.Xの機能に問題があります。私はWC_Orderに直接アクセスすることはできなくなっているが、私は関数でそれを修正する方法が不明だから(これは私が書いたものではない)、理解していると思う。WooCommerce 3+ WC_Order in backend

//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); 
    } 

私は一種の理にかなっていない運、と$order = new wc_get_order($order_id);$order = new WC_Order($post->ID);を変えてみました。私はポストIDを取得しようとしていることがわかります。ご覧のとおり、私はちょうどコードの周りに頭を抱えているので、簡単に行く。私はthis postを見ましたが、私のコードでインプリメントする方法を理解できませんでした。

機能の機能に関するフィードバックをすばやく提供するだけで、管理者の注文ページにログインしたユーザーの役割が表示されます。

+0

WooCommerce 3.0.xでは '$ order-> user_id'を' $ order-> get_user_id() 'に置き換える必要があります。オブジェクトのプロパティに直接アクセスすることができなくなり、ユーザー情報が取得されなくなります。 'DEBUG_LOG'をオンにすると、おそらくこれに関する通知が表示されます。 – helgatheviking

答えて

1

投稿IDはバックエンドで「編集後のページ」のみで取得できるため、注文の場合は「注文編集ページ」になります(「注文リストページ」ではなく)。

あなたの機能には、admin_enqueue_scriptsが含まれています。注文編集ページのみを対象にする必要があります。

WC_Orderオブジェクトを取得する必要はなく、注文ページではOrder IDがPost IDです。

注文のユーザーIDは、顧客ID(一般に「顧客」ユーザーロール)です。
また、情報のために、$user_info->roles;は配列です!

だから、正しいコードは次のようになります。

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); 
} 

コードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルやも任意のプラグインファイルになります。

他の外部ファイルが含まれているため、このコードをテストすることはできません。しかし、それは動作するはずです。

+0

それは素晴らしいです、ありがとうロイック!それははるかに意味がある、私はすべての管理者のページで実行する必要はないと思った、コメントや説明は本当に役立ちます!あなたはフリーランスですか? – Nik

関連する問題