Skip Navigation

[Resolved] Setting up a filter for a custom user role field

This thread is resolved. Here is a description of the problem and solution.

Problem:
Setting up a filter for a custom user role field - how to filer and display WooCommerce orders based on different role

Solution:
You can pass the role name as view's shortcode argument and then use the 'wpv_filter_query' filter to filter the orders based on the passed role.

You can find the proposed solution in this case with the following reply:
https://toolset.com/forums/topic/setting-up-a-filter-for-a-custom-user-role-field/#post-2258051

Relevant Documentation:
- https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created 3 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 19 replies, has 2 voices.

Last updated by martham 3 years ago.

Assisted by: Minesh.

Author
Posts
#2255255

We previously got help from Minesh in this support ticket:

https://toolset.com/forums/topic/is-there-a-field-that-will-display-a-user-role/

This was to set up a field that would display user roles as a field in a view.

The view where we have set this up is here:

hidden link

The page where we have this view displaying is here:

hidden link

I should have asked this in the other support thread, but forgot until after the 12 hour period where I could have re-opened it, so I'm starting a new thread here.

We'd like to set up a filter using this User Role field. This filter will ideally let admin end-users that have access to this report page to filter the results by the available User Roles. Given that this is being generated by Custom Code that Minesh added, I'm not sure how to even try to set it up as a filter. Is this something that would be doable?

#2255261

In addition to this field in this view, we also had another custom field created in a similar way that displays Order Product titles. We'd also like a filter set up on that field as well.

This previous support ticket is here:

https://toolset.com/forums/topic/is-there-a-woocommerce-field-that-will-display-products-ordered-in-an-order/

This is just another field in the same view so I figured I'd add this within this thread rather than creating a new thread for this. The process for setting it up as a filter should be the same.

#2255273

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

When you say you want to filter the view, do you want that you want to offer frontend filter where user should be able to filter the records when they select the role. If this is correct - there is no way to filter view based on the role as views cant offer filter to filter view results by role.

If you mean that you mean to pre-filter the results by specific role(s) that could be possible but again it will not be straight forward. We need to add custom filter and code to achieve such result.

#2255275

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

When you say you want to filter the view, do you want that you want to offer frontend filter where user should be able to filter the records when they select the role. If this is correct - there is no way to filter view based on the role as views cant offer filter to filter view results by role.

If you mean that you mean to pre-filter the results by specific role(s) that could be possible but again it will not be straight forward. We need to add custom filter and code to achieve such result.

#2255387

Yes, only our admins will have access to this page. We would like for them to be able to filter the results that appear on the view by role. We want the default to show all orders as it currently is. But then we want to add a filter on the front-end where admins can select from a drop-down list of roles and then the results provided by the view will only show ones where the role in this field matches the role chosen by the admin. Note that this filter is picking up on the data value within the User Role field and is filtering the User Roles of the customers in the report. It is not a filter that is restricted to certain User Roles being able to use it. The page itself will have the restriction keeping it only accessible by Admin users.

Then for the second one, we want to provide product name filtering. On that one, admins can choose a product and apply the filter and then only orders with that specific product ordered will display in the views result. For that one the selection process can either be all product names in a dropdown or it can be where the admin has to type in the correct product name. Either way would work.

If these two filters cannot be done with existing functionality, can you determine how much it would be for us to get someone that can code this for us. If the price isn't *too much*, we'll consider it. Or would this be something where we'll have to use your "Get help from experienced contractors" feature on the site?

#2255813

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Regarding filter orders by role:
The thing is that there is no feature available to filter the view based on the role.

Regarding filter orders by product name:
- WooCommerce stores Order Items information in a separate database table. Views cannot access that information or allow you to filter a list of Orders by the related Order Items.

This will require custom programming and for such programming work you should contact any of our certified partners:
=> https://toolset.com/contractors/

#2255837

Filtering orders by the role of the customer: Can't a relationship be established between the order and the user/customer that would allow this?

Filtering orders by product name: Since you were able to get the view to display a field showing the product ordered, I was hoping we could set up a filter on that, but understood if that's something we'd have to get custom programmed. I'm pleased you got us as far as you did with it. I've e-mailed our project manager to inquire about a cost ceiling for what she'd be willing to pay for this functionality. Depending on her response, I'll post it on the job board you have. It's possible she'll just say we don't need this and we'll let it go.

Currently I'm still trying to emulate everything previously done on the Drupal Views built versions of these pages, but understand there may be some tradeoffs for features we can't have, or require custom programming to achieve. We're almost totally done with my list of things remaining.

Separate question for you regarding a feature request I submitted. Will we hear anything back on our suggestion? This is something where I may have to post it as another job request if our project manager deems it to be essential and is willing to provide a budget for it. The suggestion was to set up a way to download Views generated reports like these as CSV or XLS files.

#2255843

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Filtering orders by the role of the customer: Can't a relationship be established between the order and the user/customer that would allow this?
==>
Here is the official Doc about how you can build relationship between post type and user:
- https://toolset.com/course-lesson/how-to-create-custom-searches-and-relationships-for-users/

But again there is a limited support. You can not add post author frontend filter that will allow users to select the author and filter the results. You can only define with what author you want to filter the results.

Maybe if that suits you, I've another suggestion, you may try to display view for each role. Lets say you can add tabs for different role and within each tab you should add view that display the orders belongs to that role. Not as you wanted but a workaround.

Separate question for you regarding a feature request I submitted. Will we hear anything back on our suggestion?
==>
Not certainly as filing feature request you can raise your voice in front of our product manager and they will review it and add it to our internal tickets for future reference but there is no ETA on when the feature you asking is implemented. You just have to keep eye on our blog to keep updated with latest news about Toolset: https://toolset.com/blog/

#2256069

I'll have to study relationships awhile before I'll be able to wrap my head around the Doc you provided.

I'm liking your suggestion with displaying different pages of results for each individual role. If we do that, we can set up something in the page content that will operate like a filter. We can set up a dropdown selection box where all the options link to the individual pages created that show the role filtered results for each role. That's a good workaround.

So I guess let's morph this ticket into getting me help setting up copies of the Member Names & Membership Levels view for each of the roles we will need. Since we only have test orders with my admin role currently present in the system can we set up one of these pages for the admin role? If I see how it is done for the admin role, I'll then be able to Duplicate the view and change the role used for all the roles we'll need.

I've created a new page and a new view to use for this. Go here for the page:

hidden link

And here for the view:

hidden link

It's named Member Names & Membership Levels - Admin on both.

All I need for help here now is how to eliminate orders from roles other than Admin role users. I want to keep the Member Names & Membership Levels main page active though so any change to the code you previously provided would have to supplement that code, not replace it. I understand that whatever is needed to limit the results in this view/page will have to be duplicated with the role name changed to limit the results in the other views I'll create for all the other roles.

#2257171

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I got the point but WooCommerce stores the order per user and there are different order status.

Have you checked this:
=> https://toolset.com/course-lesson/how-to-display-woocommerce-orders-on-the-front-end/

I see one of the order is not the completed order and that is why it does not have any users assigned. What if you filter the orders by post status - does it gives you right results?

#2257539

That order status has now been changed to Completed. That was a problem with Woocommerce not working as wanted that also caused a problem in another support thread here that one of your other colleagues is working on for me. I believe I have that problem resolved. The issue was Woocommerce doesn't move the status to completed unless it the product is set as both Virtual and Downloadable. I only had it set to Virtual. So it still showed as Processing. I've now found some code to add to functions.php that *should* make Virtual products not set as also Downloadable go straight to Completed as well. I'll test if that change is working tomorrow.

No, we don't want to have an exposed filter by order status.

All I still need is something to add to the previous code you've provided to make the page only show orders that came from users with the administrator role. I thought that was the workaround you suggested, to make separate pages for each role. Please correct me if I misunderstood your suggestion.

#2258051

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've added the following code to "Custom Code" section offered by Toolset:
=> hidden link

function func_filter_view_basedon_role( $query_args ,$view_settings, $view_id ) {
    global $WP_Views;
     
    if ( $view_id == 1752) {
     
     $args1 = array( 'fields'=>'ids',
                                'role' => $WP_Views->view_shortcode_attributes[0]['role'],
                                'orderby' => 'user_nicename',
                                'order' => 'ASC'
                            );
      $found_user_ids = get_users($args1);
      if(empty($found_user_ids)){
         $found_user_ids = array(-1);
      }
      
     $query_args['meta_key'] = '_customer_user';
	 $query_args['meta_value'] =  $found_user_ids;
      
    }
    return $query_args;
}
add_filter( 'wpv_filter_query', 'func_filter_view_basedon_role', 99, 3);

On the page, I've added the view as given under:
=> hidden link


<h1> Order for Administrator Role </h1>
[wpv-view name="member-names-membership-levels-admin" role="administrator"]

<h1> Order for Customer Role </h1>
[wpv-view name="member-names-membership-levels-admin" role="customer"]

I can see it shows the correct order for Administrator and Customer role:
=> hidden link

More info:
=> https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

#2258101

Are you still editing on the page? The system is telling me that you are editing it and it's asking me if I want to take over. I wanted to look at how you have this set up on the page.

The results look like it will be perfect on the page, but I need to see how you're creating the output. I think I'll be able to just copy and paste what you've done into each page I create and just change the role that displays to match all the roles we intend to have. But I need to see the page code before I'm sure of that. I don't want to take over editing the page until I know that you're not still editing something in there.

Thanks hugely for this as I think it's going to work based on the output I'm seeing.

#2258105

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You can take over 🙂

#2258113

OK, I've added all roles we're going to have to this page and I see that all I have to do to change the results that show is to change the role name in the <h1> tag and in the view shortcode. That should be easy.

So what I'm doing next is to create new pages for each role and then I'll add the <h1> and shortcode for only the single role per page. All the other ones won't show any results, but I'll trust that they are working since the admin one is showing the correct results.

Then I don't even have to use a different View for each one, right? I can just use the same one and it will only display the role I choose for that page on the page, correct? If so, then I should be able to have all this done sometime this afternoon (our time). Keep this thread open for now just so I can finish it all and see if I have any lingering issues, but I think this should be it if my thoughts on what I'm seeing you did are correct. Thanks hugely again.