Advertisement
dale3h

[WooCommerce] Orders - Filter by User Role

Nov 23rd, 2015
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.78 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Add filter to show WooCommerce orders from a specific user role
  5.  */
  6. if ( ! function_exists( 'wc_get_role_order_count' ) ) {
  7.     // Function to get number of orders of a user role
  8.     function wc_get_role_order_count( $role ) {
  9.         global $wpdb;
  10.  
  11.         if ( ! isset( get_editable_roles()[ $role ] ) ) {
  12.             return 0;
  13.         }
  14.  
  15.         $user_ids = get_users( array( 'role' => $role, 'fields' => 'ID' ) );
  16.  
  17.         if ( ! $user_ids ) {
  18.             return 0;
  19.         }
  20.  
  21.         $user_ids = array_map( 'absint', $user_ids );
  22.         $user_ids = implode( ',', $user_ids );
  23.  
  24.         $count = $wpdb->get_var( $sql = "
  25.             SELECT COUNT(*)
  26.             FROM {$wpdb->posts} as posts
  27.  
  28.             LEFT JOIN {$wpdb->postmeta} AS meta ON posts.ID = meta.post_id
  29.  
  30.             WHERE   meta.meta_key       = '_customer_user'
  31.             AND     posts.post_type     IN ('" . implode( "','", wc_get_order_types( 'order-count' ) ) . "')
  32.             AND     posts.post_status   IN ('" . implode( "','", array_keys( wc_get_order_statuses() ) )  . "')
  33.             AND     meta.meta_value     IN ({$user_ids})
  34.         " );
  35.  
  36.         return absint( $count );
  37.     }
  38. }
  39.  
  40. add_action( 'restrict_manage_posts', 'shop_order_user_role_filter' );
  41.  
  42. function shop_order_user_role_filter() {
  43.     global $typenow;
  44.  
  45.     if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ) ) ) {
  46.         $user_role = '';
  47.  
  48.         // Get all user roles
  49.         $user_roles = array();
  50.  
  51.         foreach ( get_editable_roles() as $key => $values ) {
  52.             // Only show the role if it has orders
  53.             if ( wc_get_role_order_count( $key ) > 0 ) {
  54.                 $user_roles[ $key ] = $values['name'];
  55.             }
  56.         }
  57.  
  58.         // Set a selected user role
  59.         if ( ! empty( $_GET['_user_role'] ) ) {
  60.             $user_role = sanitize_text_field( $_GET['_user_role'] );
  61.         }
  62.  
  63.         // Display drop down
  64.         ?><select name="_user_role">
  65.             <option value=""><?php _e( 'Select a user role', 'woocommerce' ); ?></option><?php
  66.             foreach ( $user_roles as $key => $value ) {
  67.                 ?><option <?php selected( $user_role, $key ); ?> value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $value ); ?></option><?php
  68.             }
  69.         ?></select><?php
  70.     }
  71. }
  72.  
  73. add_filter( 'request', 'shop_order_user_role_request_query' );
  74.  
  75. function shop_order_user_role_request_query( $vars ) {
  76.     global $typenow;
  77.  
  78.     if ( ! empty( $_GET['_user_role'] ) && in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ) ) ) {
  79.         // Get user IDs of role
  80.         $ids = get_users( array( 'role' => sanitize_text_field( $_GET['_user_role'] ), 'fields' => 'ID' ) );
  81.         $ids = array_map( 'absint', $ids );
  82.  
  83.         // Make sure we have a valid array to work with
  84.         if ( ! isset( $vars['meta_query'] ) || ! is_array( $vars['meta_query'] ) ) {
  85.             $vars['meta_query'] = array();
  86.         }
  87.  
  88.         // Add query to check for _customer_user
  89.         $vars['meta_query'][] = array(
  90.             'key'     => '_customer_user',
  91.             'value'   => $ids,
  92.             'compare' => 'IN',
  93.         );
  94.     }
  95.  
  96.     return $vars;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement