title: WooCommerce sort order items by categories
body: <p><strong><strong>{UPDATE}</strong></strong></p>
  <p>I want to sort products on backend order details by category menu order. So my categories are Clothing, Decor, Music, and products must be ordered in this way.</p>
  <p>I tried with this snippet</p>
  <pre><code>add_filter( 'woocommerce_order_get_items', 'filter_order_get_items_by_categories12', 10, 3 );
  function filter_order_get_items_by_categories12($items, $order) {
      global $woocommerce, $wpdb;
  
          $c1 = $wpdb-&gt;get_results( &quot;select t1.order_item_name,t1.order_item_id,t5.name,t2.meta_value from
          wp_woocommerce_order_items as t1 LEFT JOIN wp_woocommerce_order_itemmeta as t2 ON t1.order_item_id =
          t2.order_item_id LEFT JOIN wp_term_relationships as t3 ON t2.meta_value = t3.object_id LEFT JOIN wp_term_taxonomy
          as t4 ON t3.term_taxonomy_id = t4.term_taxonomy_id LEFT JOIN wp_terms as t5 ON t4.term_id = t5.term_id where
          t1.order_id = '&quot; . $order-&gt;get_id() . &quot;' and t1.order_item_type='line_item' and t2.meta_key='_product_id' and
          t4.taxonomy='product_cat' ORDER BY `t5`.`name` ASC&quot; );
           
          $sorted_item_ids     = array();
          
          foreach ( $c1 as $data ) {
              if ( !in_array( $data-&gt;order_item_id, $sorted_item_ids ) ) {
  
                  $sorted_item_ids[] = $data-&gt;order_item_id;
              }
          }
          if( count($items) &gt; 1 ) {
              $item_categories = $sorted_items_ids = array();
              
              foreach ($items as $item_id =&gt; $item) {
    
        // Get the product category(ies) term(s) name(s) - (array)
          $term_ids = wp_get_post_terms( $item-&gt;get_product_id(), 'product_cat', array('fields' =&gt; 'ids') );
  
              }
  
              foreach ( $term_ids as $term ) {
              // Categories by slug
               $product_cat_id= $term-&gt;id;
              }
              $item_categories[$product_cat_id] = $item_id;
          } 
          
          $query = new WP_Query( array(
         'posts_per_page'  =&gt;  -1,
         'post_type'       =&gt;  array( 'shop_order' ), // &lt;== HERE MISSING
      ) );
     
  
      // Loop in the Query
    
              // Get the post ID
               $post_id = $query-&gt;post-&gt;ID;
  
              // Get the corresponding item ID for the current product ID
              $item_id = $item_categories[ $post_id ];
  
              // Get the new sorted array of items
              $sorted_items_ids[$item_id] = $items[$item_id];
  
      wp_reset_query();
  
      return $sorted_items_ids;
  }
  </code></pre>
  <p>This version give me first item how must be, but gives error
  <a href="https://i.stack.imgur.com/PLCNw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PLCNw.png" alt="enter image description here" /></a></p>
  <p>Error is</p>
  <pre><code>Fatal error: Uncaught Error: Call to a member function get_name() on null in /home/wordcamp16870/www/vladi/wp-content/plugins/woocommerce/includes/admin/meta-boxes/views/html-order-fee.php:18 Stack trace: #0 /home/wordcamp16870/www/vladi/wp-content/plugins/woocommerce/includes/admin/meta-boxes/views/html-order-items.php(72): include() #1 /home/wordcamp16870/www/vladi/wp-content/plugins/woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-items.php(41): include('/home/wordcamp1...') #2 /home/wordcamp16870/www/vladi/wp-admin/includes/template.php(1389): WC_Meta_Box_Order_Items::output(Object(WP_Post), Array) #3 /home/wordcamp16870/www/vladi/wp-admin/edit-form-advanced.php(688): do_meta_boxes(Object(WP_Screen), 'normal', Object(WP_Post)) #4 /home/wordcamp16870/www/vladi/wp-admin/post.php(206): require('/home/wordcamp1...') #5 {main} thrown in /home/wordcamp16870/www/vladi/wp-content/plugins/woocommerce/includes/admin/meta-boxes/views/html-order-fee.php on line 18
  There has been a critical error on this website. Please check your site admin email inbox for instructions.
  </code></pre>
  <p>Any help will be appreciated!! Thanks in advance!</p>