How can I assign an order to a certain shop manage

2019-05-18 19:27发布

问题:

we have a services woocommerce online shop with three shop managers.

We would like to filter new orders and assign them to one of these three managers. The managers only can see their assigned orders, and can't access or see the rest.

Maybe this could be done by filtering the backend view (admin panel) via custom_field, but I don't know if it is a good approach. Maybe there is a plugin based on role capabilites.

Any suggest?

Thanks.

回答1:

After having the same issue I combined the solutions of similar, already answered questions and it worked. I know the question is old, but someone may find it helpful. Here is one possible solution:

  1. Add custom meta key containing your desired store_manager id to every order (in my case if a order was from a specific country I wanted only a sprecific store_manager to see it, you can put your custom logic in the before_checkout_create_order() function)


    function before_checkout_create_order($order, $data) {

        $country = $order->billing_country;
        $store_manager_id = '';

        $belgium_region = ['BE', 'NL', 'DE'];
        $czech_region = ['CZ', 'AT', 'SI', 'HU'];
        $uk_region = ['GB'];

        if (in_array($country, $belgium_region)) {
            // Manually assigning the _store_manager_id using the user id, yours will differ
            $store_manager_id = 7;
        } else if (in_array($country, $czech_region)) {
            $store_manager_id = 3;
        } else if (in_array($country, $uk_region)) {
            $store_manager_id = 2;
        } else {
            $store_manager_id = 1;
        }

        $order->update_meta_data('_store_manager_id', $store_manager_id);
    }
    add_action('woocommerce_checkout_create_order', 'before_checkout_create_order', 20, 2);

I found the method for assigning custom meta keys to a order ON THIS THREAD you can see what the woocommerce_checkout_create_order and $order->update_meta_data() hook and method do there, it's already greatly explained by its author

  1. Filter the Woocommerce Admin Order list to only show current user assigned orders (but let Master Admin view all orders also)


    function custom_admin_shop_manager_orders($query) {
        global $pagenow;
        $qv = &$query->query_vars;

        $currentUserRoles = wp_get_current_user()->roles;
        $user_id = get_current_user_id();

        if (in_array('shop_manager', $currentUserRoles)) {
            if ( $pagenow == 'edit.php' && 
                    isset($qv['post_type']) && $qv['post_type'] == 'shop_order' ) {
                // I use the meta key from step 1 as a second parameter here
                $query->set('meta_key', '_store_manager_id');
                // The value we want to find is the $user_id defined above
                $query->set('meta_value', $user_id);
            }
        }

        return $query;
    }
    add_filter('pre_get_posts', 'custom_admin_shop_manager_orders');

As you can see we check if the current user role is shop_manager so all the logic after that doesn't count if you're the admin - you will see all the orders, but if you are a shop_manager you will get only your orders. The edit.php is the woocommerce orders list page.

I found the second step in THIS THREAD so you can found more info there also.

Hope this helps you!



回答2:

This function makes the orders display only to its own authors (shop managers):

function alter_the_edit_screen_query( $wp_query ) {
    if ( ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php' ) !== false ) and ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php?post_type=unit' ) === false ) and ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php?post_type=course' ) === false ) ) {
        if ( !current_user_can( 'activate_plugins' ) )  {
add_action( 'views_edit-post', 'remove_items_from_edit' );
            global $current_user;
            $wp_query->set( 'author', $current_user->id );
        }
    }
}

add_filter('parse_query', 'alter_the_edit_screen_query' );

function remove_items_from_edit( $views ) {
    unset($views['all']);
    unset($views['publish']);
    unset($views['trash']);
    unset($views['draft']);
    unset($views['pending']);
    return $views;
}

This other function hides "All orders" tab to no-admin users (as shop manager):

function my_custom_admin_head() {
   if ( ! current_user_can( 'update_core' ) ) {
    echo '<style type="text/css">
    ul.subsubsub li.all { display:none!important; }
    </style>';
}
}
add_action( 'admin_head', 'my_custom_admin_head' );