0

We are using User Role Editor Pro to manage user roles. We want to only display orders belonging to the respective "salesagent" (role).

I have been able to pull in the current user role and get access to the order columns on the correct page. I do not see any role information on the order: https://gist.github.com/leafdropco/2ae7281cc81864e74af28c507f3c2ad0

How can i only show orders to the correct agents?

// Hide products from agents that dont own them
add_action( 'manage_shop_order_posts_custom_column', 'display_sales_agent_orders_only' );
function display_sales_agent_orders_only( $columns ) {
    global $the_order, $post;
    
    if( is_user_logged_in() ) { // check if there is a logged in user 
        $user = wp_get_current_user(); // getting & setting the current user 
        $roles = ( array ) $user->roles; // obtaining the role
        if(in_array('sales_agent', $roles)) {
            $salesagentid = '60802fa984f32';
            if ( $salesagentid === $columns ) {
                print_r($columns);
            }
        }
    }
}
  • Hello, what do you mean by "only display orders belonging to the respective "salesagent" (role)"? Is the salesagent the current customer? Is a saleagent another user linked to the customer? What is the final objective here? – Mtxz Sep 28 '21 at 01:13
  • User Role Editor allows you to manage additional user roles. "sales_agent" is the role in which the agents use. If the order doesn't belong to the agent, then dont show the order. The problem im having is that I cant seem to find how the order and user are linked. on the woocommerce>orders page the sales agent data is in the table, but if i print the order, i see no reference to the sales_agent or any role. @Mtxz – Michael Christopher Martire Sep 28 '21 at 01:51
  • Ah ok, so you have to query the "current user orders". You can do it in at least 2 ways: woocommerce way with `wc_get_orders` and user_id parameter (see https://stackoverflow.com/questions/42643065/how-to-get-all-orders-of-current-user-in-woocommerce), or WordPress way with a wp_query (see https://stackoverflow.com/questions/51980040/get-all-orders-and-orderdata-of-a-single-customer-in-woocommerce-by-user-id/51981887) - is this what you need? – Mtxz Sep 28 '21 at 01:58
  • If you want to retrieve the user_id of an order, see $order->get_user() or $order->get_user_id() (see https://wordpress.stackexchange.com/questions/138123/woocommerce-how-to-get-the-user-belonging-to-an-order) – Mtxz Sep 28 '21 at 02:01
  • There is a big problem for you. What happens if i add products from 2 agents in same order ? So instead of focusing on order its self focus on items in your order. I have made a few franchise systems for clients and always go with product author. Then either i split order (if more than one seller ) that way i pull orders by items. – Snuffy Sep 29 '21 at 08:49
  • @MichaelChristopherMartire The current code you are using **manages the columns**, while I think you want to **hide certain rows** in the WooCommerce admin orders list table? If so, that's your first mistake... – 7uc1f3r Sep 29 '21 at 11:50

2 Answers2

0

To get the customer from an order, you can do:

1. Woocommerce ways

wc_get_orders and user_id parameter (see)

$args = array(
    'customer_id' => $user_id
);
$orders = wc_get_orders($args);

2. WordPress way

With a custom wp_query on the Order post, and _customer_user post meta

$customer = wp_get_current_user();
// Get all customer orders
    $customer_orders = get_posts(array(
        'numberposts' => -1,
        'meta_key' => '_customer_user',
        'orderby' => 'date',
        'order' => 'DESC',
        'meta_value' => get_current_user_id(),
        'post_type' => wc_get_order_types(),
        'post_status' => array_keys(wc_get_order_statuses()),
    ));

3 - get the user from an order Object

If you want to retrieve the user_id of an order, see $order->get_user() or $order->get_user_id() (see)

Mtxz
  • 3,749
  • 15
  • 29
0

Here is what worked for me:

add_action( 'pre_get_posts', 'admin_pre_get_posts_orders_query', 9999 );
function admin_pre_get_posts_orders_query( $query ) {
    global $pagenow;
    if (current_user_can('edit_others_shop_orders')) { return $query; }
    // Targeting admin orders list
    if( 'edit.php' == $pagenow &&
        $query->get( 'post_type' ) == 'shop_order'
      ) {
          $query->set( 'meta_query', array( array(
                'key' => 'wcb2bsa_sales_agent', 
                'value' => get_current_user_id() ) ) ); // Only displays the orders created by the current user
    }
}